Jump to content


Photo

Compare dates?


  • Please log in to reply
8 replies to this topic

#1 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 25 August 2006 - 02:23 PM

How can you compare dates?

I have both of them in "Y-m-d H:i:s" format.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 02:25 PM

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?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 devbanana

devbanana
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationI don't know, but all I can see is this computer...

Posted 25 August 2006 - 02:28 PM

Use strtotime() on both dates, then compare the resulting timestamps.
Programming in PHP since 1882. Oh wait, computers weren't around that long, huh....
Hey, while we're here, would you mind giving me some suggestions on my PHP framework?

#4 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 25 August 2006 - 02:32 PM

I want to get the diffrence in hours. Ho can I do that with the unix timestamp that devbanana pointed out?

#5 devbanana

devbanana
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationI don't know, but all I can see is this computer...

Posted 25 August 2006 - 02:36 PM

Well, if you subtract them, the difference will be in seconds. If you divide by 3600, you should have the difference in hours.

$diff = $date2 - $date1;
$hours = floor($diff / 3600);

Programming in PHP since 1882. Oh wait, computers weren't around that long, huh....
Hey, while we're here, would you mind giving me some suggestions on my PHP framework?

#6 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 25 August 2006 - 02:49 PM

So why dosn't this code work?

$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;

It outputs:

Hour differance:642507


Why is that? It is only 2 hours!

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 25 August 2006 - 02:52 PM

$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;

Legend has it that reading the manual never killed anyone.
My site

#8 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 25 August 2006 - 02:56 PM

Thanks!

I missed that ::) ::)

#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 02:56 PM

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:
<?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;
?>

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users