ari_aaron Posted August 25, 2006 Share Posted August 25, 2006 How can you compare dates?I have both of them in "Y-m-d H:i:s" format. Quote Link to comment Share on other sites More sharing options...
obsidian Posted August 25, 2006 Share Posted August 25, 2006 how do you mean "compare"? in what way are you comparing them? just seeing if they are equal or getting the difference between them, or what? Quote Link to comment Share on other sites More sharing options...
devbanana Posted August 25, 2006 Share Posted August 25, 2006 Use [url=http://php.net/strtotime]strtotime()[/url] on both dates, then compare the resulting timestamps. Quote Link to comment Share on other sites More sharing options...
ari_aaron Posted August 25, 2006 Author Share Posted August 25, 2006 I want to get the diffrence in hours. Ho can I do that with the unix timestamp that [b]devbanana[/b] pointed out? Quote Link to comment Share on other sites More sharing options...
devbanana Posted August 25, 2006 Share Posted August 25, 2006 Well, if you subtract them, the difference will be in seconds. If you divide by 3600, you should have the difference in hours.[code=php:0]$diff = $date2 - $date1;$hours = floor($diff / 3600);[/code] Quote Link to comment Share on other sites More sharing options...
ari_aaron Posted August 25, 2006 Author Share Posted August 25, 2006 So why dosn't this code work?[code]$stoday=strtotime("2006-08-25 10:47:09");$sdate=-strtotime("2006-08-25 08:58:01");$diff = $stoday - $sdate;$hours = floor($diff / 3600);echo "Hour differance:" . $hours;[/code]It outputs:[quote]Hour differance:642507[/quote]Why is that? It is only 2 hours! Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 25, 2006 Share Posted August 25, 2006 [code]$stoday = strtotime("2006-08-25 10:47:09");$sdate = strtotime("2006-08-25 08:58:01"); // negative sign removed!!$diff = $stoday - $sdate;$hours = floor($diff / 3600);echo "Hour differance:" . $hours;[/code] Quote Link to comment Share on other sites More sharing options...
ari_aaron Posted August 25, 2006 Author Share Posted August 25, 2006 Thanks!I missed that ::) ::) Quote Link to comment Share on other sites More sharing options...
obsidian Posted August 25, 2006 Share Posted August 25, 2006 you've got your second time with a negative in front of it, so you're actually adding the two times together. try writing up a simple function to do it for you, like this:[code]<?phpfunction get_hours($ts1, $ts2) { $diff = abs(strtotime($ts1) - strtotime($ts2)); return round($diff / 3600);}$stoday = "2006-08-25 10:47:09";$sdate = "2006-08-25 08:58:01";$hours = get_hours($stoday, $sdate);echo "Hour differance: " . $hours;?>[/code]also, notice that i'm using round() instead of floor. this will give you a more accurate representation of the time. for instance, in the times you're playing with, floor() will only return 1 hour difference while ceil() or round() will give you 2. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.