-1

I have a date formatted like this: Ymd

I can't seem to find a way to be able to add a number of weeks to this date, I have tried the following:

$quote_start_date = $job['quote_authorised'];

$newdate = date($quote_start_date, strtotime('+5 weeks'));

However the new date is the same, what is the easiest way to add weeks to a date formatted like this?

Aaranihlus
  • 143
  • 2
  • 13

3 Answers3

1

The 2nd parameter to date takes seconds since epoch. Just add 5 weeks in seconds to the time, ie:

$newdate = date($format, strtotime($quote_start_date) + (5 * 7 * 24 * 60 * 60));

Or just use the constant value "3024000"

$newdate = date($format, strtotime($quote_start_date) + 3024000);
HostFission
  • 374
  • 1
  • 7
1

The first parameter of date expects a format for your outputted date string. I think you're looking for the following:

$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime('+5 weeks', strtotime($quote_start_date)));

The more efficient approach would be to use a fixed value for the number of seconds in a week and not rely on PHP parsing an additional strtotime function:

$quote_start_date = $job['quote_authorised'];
$newdate = date("Ymd", strtotime($quote_start_date) + 3024000);
kinggs
  • 1,162
  • 2
  • 10
  • 25
  • This is extremely ineffecient, you are asking PHP to parse '+5 weeks' which will just return a constant value of `3024000`. Just add 3024000 to the first `strtotime` call. – HostFission Sep 13 '17 at 08:38
0

Its a demo :

 $date = new Date();
 $nextDate = new Date($date.setTime( $date.getTime() + 1 * 86400000 ));
 // here 1 is number of day .
Minar Mnr
  • 1,376
  • 1
  • 16
  • 23