Page 1 of 1

Calculate previous and next date from a variable

Posted: Fri Mar 04, 2005 8:02 am
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

Posted: Fri Mar 04, 2005 8:08 am
by n00b Saibot
Previous Day : strtotime("-1 day")
Next Day : strtotime("+1 day")

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

Posted: Fri Mar 04, 2005 8:23 am
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))));

Posted: Fri Mar 04, 2005 8:29 am
by feyd
nOOb gave you a working, simple solution.. :?

Posted: Fri Mar 04, 2005 8:35 am
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 ;)