Calculate previous and next date from a variable

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
lhelmer
Forum Newbie
Posts: 3
Joined: Mon Dec 15, 2003 10:09 am

Calculate previous and next date from a variable

Post by lhelmer »

Hello,

I need some help determining the next and previous dates from a variable. So far this is what I have tried but it keeps returning 1970-01-01

Code: Select all

$PreviousDay = date("Y-m-d",mktime(0,0,0,date("m",$CurrentTime) ,date("d",$CurrentTime)-1,date("Y",$CurrentTime)));
Note: $CurrentTime = 2005-03-04

Can anyone tell me why this doesn't return 2005-03-03 and more inportantly how to correctly calculate the next and previous days?

Thanks,
Lou
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Previous Day : strtotime("-1 day")
Next Day : strtotime("+1 day")
lhelmer
Forum Newbie
Posts: 3
Joined: Mon Dec 15, 2003 10:09 am

This may not be the best coding... I'm new at this...

Post by lhelmer »

I found a solution... If this is a bad way to solve the problem please post back a better example.

Thanks,
Lou

Code: Select all

$PreviousDay = date("Y-m-d",mktime(0,0,0,date("m",strtotime($CurrentTime)) ,date("d",strtotime($CurrentTime))-1,date("Y",strtotime($CurrentTime))));
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nOOb gave you a working, simple solution.. :?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Correct Code wrote: <?
$PrevDay
= date("Y-m-d",strtotime("-1 day"));
$NextDay = date("Y-m-d",strtotime("+1 day"));
?>
Of course, the simplest solution is the previous one given.


EDIT: Was only trying PHPDN Hi-liter, Sure works great
Thanks, d11wtq. was missing colors ;)
BTW, I've hit Professional :D jolly good,eh ;)
Post Reply