mtorbin Posted April 16, 2009 Share Posted April 16, 2009 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 More sharing options...
mtorbin Posted April 16, 2009 Author Share Posted April 16, 2009 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. Link to comment https://forums.phpfreaks.com/topic/154382-solved-comparing-unix-time-stamps/#findComment-811687 Share on other sites More sharing options...
mtorbin Posted April 16, 2009 Author Share Posted April 16, 2009 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 Link to comment https://forums.phpfreaks.com/topic/154382-solved-comparing-unix-time-stamps/#findComment-811699 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 These dates are not unix timestamps. They're strings. [added after mtorbin has found a solution by himself] Glad we could help Link to comment https://forums.phpfreaks.com/topic/154382-solved-comparing-unix-time-stamps/#findComment-811702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.