Jump to content

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

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.