comparing dates

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
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

comparing dates

Post 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!
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Try using

Code: Select all

strtotime
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

I have used strtotime but it is very inacturate when calculating dates.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

how so? as long as your input format is going to be consistent I don't see what inaccuracies could happen.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

if you're using db then you could use mysql functions to calculate difference between dates, like DAYDIFF
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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.
Post Reply