Jump to content

[SOLVED] Comparing UNIX Time Stamps


mtorbin

Recommended Posts

I have a class I've built who's main goal is compare unix time stamps and reorder them in an array accordingly.  I have them reordering ok, but I think the ordering determination is a big askew.  Here is the function:

function compareDates($firstDate, $secondDate) {
if($firstDate[1] == $secondDate[1]) {
	return 0;
}
else if ($firstDate[1] < $secondDate[1]) {
	return 1;
}
else {
	return -1;
}
}

 

which is initiated here:

usort($assetList, 'compareDates');

 

Here is a sample of what the array might look like:

 

$assetList[0][0] = "2008-02-06T07:53:55.297-04:00";
$assetList[0][1] = "080605_pc_PBT_retailup";
$assetList[1][0] = "2008-03-04T14:01:48.293-04:00";
$assetList[1][1] = "080604_pc_PBT_maria_sludge";
$assetList[2][0] = "2009-06-03T12:26:22.773-04:00";
$assetList[2][1] = "080603_pc_PBT_hummer";
$assetList[3][0] = "2008-06-06T12:16:22.157-04:00";
$assetList[3][1] = "080602_pc_PBT_wachovia2";
$assetList[4][0] = "2007-05-09T13:37:59.697-04:00";
$assetList[4][1] = "080530_pc_PBT_redlasso";

 

Is this the best way to do this?  Why are the dates coming back incorrectly sorted?

 

Thanks,

 

- MT

Link to comment
https://forums.phpfreaks.com/topic/154382-solved-comparing-unix-time-stamps/
Share on other sites

Please note there is a typo above.  The function should read:

function compareDates($firstDate, $secondDate) {
if($firstDate[0] == $secondDate[0]) {
	return 0;
}
else if ($firstDate[0] < $secondDate[0]) {
	return 1;
}
else {
	return -1;
}
}

 

This does not resolve the problem however.

D'oh!  Sometimes it just helps to type things out.  Here's what the function should look like:

function compareDates($firstDate, $secondDate) {
$firstDate = strtotime($firstDate[0]);
$secondDate = strtotime($secondDate[0]);

if($firstDate == $secondDate) {
	return 0;
}
else if ($firstDate < $secondDate) {
	return 1;
}
else {
	return -1;
}
}

 

Thanks for the forum!

 

- MT

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.