Page 1 of 1

how to get get the next/previous date

Posted: Tue Mar 15, 2005 10:14 am
by jasongr
Hi

Assuming I have a specific date in 3 variables:
$day (1-31)
$month (1-12)
and $year (1900-2100)

I would like to know what the date would be one day from now or what it was the day before.
Or what would be the date one month from now and one month before
Or what would be the date one year from now and one year before

Is there a way to get this information using Date/Time PHP functions without resulting to explicit calculations (if I am in February there are 28 days, except...)

For example, if the date is:
$day = 31
$month = 3
$year = 2005
Then one day from now it would be 1/4/2005

regards

Posted: Tue Mar 15, 2005 10:26 am
by pickle
The function you're looking for is strtotime(). Here's how I'd do it:

Code: Select all

$day = 02;
$month = 11;
$year = 2222;

$future_day = strtotime('+1 day',strtotime($year . $month . $day))
and so on with the +1 month, +1 year, etc

Posted: Tue Mar 15, 2005 10:26 am
by JayBird
This should sort you out...i dont think pickles example works....return -1 for me.

Code: Select all

$day = 02;
$month = 11;
$year = 1999; 

echo date("d-m-Y", strtotime("+1 day", mktime(0, 0, 0, $month, $day, $year)));

Posted: Tue Mar 15, 2005 10:42 am
by pickle
Your obviously right - mine doesn't work. The strtotime documentation says it can take GNU date input format. A valid pure number format description can be found here: http://www.gnu.org/software/tar/manual/ ... tml#SEC116


Did someone lie to me :evil: or am I an idiot? In either case, ~Pimptastic's will work too.

Posted: Tue Mar 15, 2005 10:43 am
by jasongr
Thanks guys, I learned something new
that solved my problem