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!
Asked
Active
Viewed 460 times
-2
-
1this isn't a bad question. why all the downvotes? – Loïc Apr 22 '14 at 03:03
1 Answers
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