1

I have a date in php using the date function

$date =  date('Y-m-d');

I need to add certain number of days to the following month to show a deadline.

eg: if someone adds a job today with a 5 days, 2014-08-04 the deadline should be 2014-09-06

I was planning to get the current month using the date function and increment the value by 1 to go to the next month and then add the number of days to the day in the date function.

This looks lengthy and not that right

Can someone tell me What would be the best way to achieve this?

John Conde
  • 217,595
  • 99
  • 455
  • 496
LiveEn
  • 3,193
  • 12
  • 59
  • 104

2 Answers2

1

Just use DateTime():

$date = (new DateTime('2014-08-04'))->modify('next month')->modify('+5 days')->format('Y-m-d');

or with DateInterval():

$date = (new DateTime('2014-08-04'))->modify('next month')->add(new DateInterval('P5D'))->format('Y-m-d');

or, perhaps better suited to your needs:

$days = 5;
$date = (new DateTime())->modify('next month')->modify("{$days} days")->format('Y-m-d'); 

Both of the above require PHP 5.4+. If you need this converted to PHP 5.3 just let me know.

Here is a PHP 5.3 version of the above code:

$days = 5;
$date = new DateTime();
$date->modify("next month"); 
$date->modify("{$days} days"); 
$date = $date->format('Y-m-d');
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • *"eg: if someone adds a job today with a 5 days"* - Looks like a trick question. OP may be looking for `if($days_entered == 5){...}` ;) – Funk Forty Niner Aug 04 '14 at 18:23
  • It does look like that value is dynamic. Hopefully this points them in the right direction. I *think* it does. – John Conde Aug 04 '14 at 18:23
  • I think it does too. I just don't want you to get burned ;) – Funk Forty Niner Aug 04 '14 at 18:24
  • 1
    My Italian olive skin helps protect me from that :) – John Conde Aug 04 '14 at 18:24
  • *"If you need this converted to PHP 5.3 just let me know."* - You signore, are too kind ;) – Funk Forty Niner Aug 04 '14 at 19:27
  • @JohnConde thanks. can you please tell how can i do it in php 5.3? my php version is 5.3 :( and in the above code does it move to the next month and adds the days or adds the days to the current date. I dont need to do any comparing, i just need move to the next month and add the date. – LiveEn Aug 05 '14 at 14:26
  • I added the PHP 5.3 code. I also changed it so it automatically adds a month as well. – John Conde Aug 05 '14 at 14:30
  • @JohnConde thanks, this works but there is a small problem. If i run this code it adds the used days in the previous month. eg: if i add the date `2014-08-05` with 5 days i would need the output as `2014-09-05` but it shows `2014-09-10`. any idea how to fix it? – LiveEn Aug 05 '14 at 14:37
  • Are you trying to add days? or months? If you don't want the addecd days just get rid of that part of the code. – John Conde Aug 05 '14 at 14:38
  • @JohnConde i need to add a month and days. its like this, if someone adds a job this month, the job starts from next month. so the days should be added to the next month freshly. So all jobs added on August should start adding the days from 1st of september – LiveEn Aug 05 '14 at 14:48
  • Ok, this is my last crack at it. Even if it's not right this should be more than enough to show you how to do it. – John Conde Aug 05 '14 at 14:52
1

The PHP community is fortunate to have such a powerful core of available functions built right in. There are innumerable ways to solve this, but the simplest is often best.

strtotime has been supported since version 4. It is quick. It is clear. It is highly maintainable. There is no need for anything more complex.

$future_stamp = strtotime('+ 1 month 5 days');
$date = date('Y-m-d', $future_stamp);

EDIT:

After much reading and guesswork, this is probably what you actually want. Also, feel obliged to read the strtotime documentation. You could have figured this out.

$future_stamp = strtotime(date('Y-m-d', strtotime('first day of next month')) . ' + 5 days');
danemacmillan
  • 1,202
  • 12
  • 13
  • thanks for the code, but there is a small problem. if i add the date `2014-08-05` with 5 days i would need the output as `2014-09-05` but in in your code it shows `2014-09-10` it also calculates the days used from the previous month. any idea how to add the days freshly to the next month? – LiveEn Aug 05 '14 at 14:33
  • @LiveEn, you're not being clear. Adding five days to 2014-08-05 would be 2014-08-10. Adding a month and five days to 2014-08-05 would be 2014-09-10. Do you mean to say that any posts made this month, should only begin displaying at the first of the next month, plus the additional days (e.g., 5 days)? If that's the use in mind, read the edit. – danemacmillan Aug 05 '14 at 15:58