Page 1 of 1

comparing dates

Posted: Fri Jan 05, 2007 9:21 am
by kingconnections
Ok so this has been bugging me for sometime now. It seems if I am correct that any time you want to compare dates you have to do a mktime function. Why if I have a given date already, and can format it do i need to do a mktime function? Example: If I wanted to find out what yesterday was I could do this:


Code: Select all

$timestamp_now2=mktime(12,0,0,1,5-1,2007);
echo "<br>".date('d M Y',($timestamp_now2 ));
But what If I already had yesterdays date in a db in standard datetime format. In order to acurately compare dates I have to parse it and use mktime?


Thanks
again!

Posted: Fri Jan 05, 2007 9:26 am
by impulse()
Try using

Code: Select all

strtotime

Posted: Fri Jan 05, 2007 9:41 am
by kingconnections
I have used strtotime but it is very inacturate when calculating dates.

Posted: Fri Jan 05, 2007 9:42 am
by Kieran Huggins
how so? as long as your input format is going to be consistent I don't see what inaccuracies could happen.

Posted: Fri Jan 05, 2007 9:47 am
by feyd
strtotime() will have trouble understanding certain formats because they aren't very uniform and can be interpreted in too many ways. "d M Y" is likely a good example of this, whereas "Y-m-d" works just fine.

Posted: Fri Jan 05, 2007 10:02 am
by kingconnections
Yep that was the example I was going to use. Sometimes it would return like some crazy value like 1970 blah blah when the real date was last week LOL.

Posted: Fri Jan 05, 2007 10:03 am
by kingconnections
So if I revise the input to strtotime to Y-m-d then that should solve some of the issues I am seeing? I really was just trying to get a feel for how other people calculate dates. It would seem to me that this is a common issue that people would come across.

Posted: Fri Jan 05, 2007 11:22 am
by Kieran Huggins
strtotime() is pretty flexible, just as long as there's no ambiguity.

The whole YY-MM-DD issue is the biggest, really.

Posted: Fri Jan 05, 2007 2:47 pm
by arukomp
if you're using db then you could use mysql functions to calculate difference between dates, like DAYDIFF

Posted: Fri Jan 05, 2007 2:50 pm
by kingconnections
That is how I have been working around this right now actually. But it just bugged me, why it was doing that. There have been some times where the sql query was so complex that it was hard to fit in the date diff function. Ie items come from 2 different sql servers and you are comparing dates.