Warm tip: This article is reproduced from serverfault.com, please click

Adding days to $Date in PHP

发布于 2010-09-16 14:23:07

I have a date returned as part of a mySQL query in the form 2010-09-17

I would like to set the variables $Date2 to $Date5 as follows:

$Date2 = $Date + 1

$Date3 = $Date + 2

etc..

so that it returns 2010-09-18, 2010-09-19 etc...

I have tried

date('Y-m-d', strtotime($Date. ' + 1 day'))

but this gives me the date BEFORE $Date.

What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?

Questioner
Istari
Viewed
0
shamittomar 2010-09-16 22:46:41

All you have to do is use days instead of day like this:

<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>

And it outputs correctly:

2010-09-18
2010-09-19