-2

I need to be able to count forward X number of dates into the future in PHP. For example, 14 days from today is what date? What routines in PHP can be used for this? Thanks!

Edward Coast
  • 374
  • 4
  • 17

1 Answers1

6

This is easy with date() and strtotime():

echo date('Y-m-d', strtotime('+14 days'));

You can also use DateTime:

$date = new DateTime();
$date->modify('+14 days');
echo $date->format('Y-m-d');

Or:

$date = new DateTime('+14 days');
echo $date->format('Y-m-d');

Or as a one-liner:

echo (new DateTime('+14 days'))->format('Y-m-d');

Or with DateInterval:

$date = new DateTime();
$date->add(new DateInterval('P14D'));
echo $date->format('Y-m-d');
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • `echo (new DateTime('+14 days'))->format('Y-m-d');` not the type of "one-liner" I'd give a brunette, that's for sure (grin). +1 though on the "coding side" of it. ;-) – Funk Forty Niner Apr 22 '14 at 03:59