how to get get the next/previous date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to get get the next/previous date

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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
Last edited by pickle on Tue Mar 15, 2005 10:26 am, edited 1 time in total.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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)));
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

Thanks guys, I learned something new
that solved my problem
Post Reply