Jump to content

Compare dates?


ari_aaron

Recommended Posts

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!
Link to comment
Share on other sites

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]
<?php
function 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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.