Re: [PHP] Sorting times (SOLVED)
Just a draft i thought should not go unnoticed on the list :-) just cleaning up. OK, How about a super efficient soln where each string is only converted once and a fast sorting algorithm is used: ?php function time_sort($a, $b) { static $now = time(); if (strtotime($a, $now) == strtotime($b, $now)) { return 0; } return (strtotime($a, $now) strtotime($b, $now) ? -1 : 1; } function sortTime($times) { } Tim-Hinnerk Heuer http://www.ihostnz.com Fred Allen - California is a fine place to live - if you happen to be an orange. 2009/2/16 Shawn McKenzie nos...@mckenzies.net tedd wrote: At 9:31 PM -0600 2/14/09, Shawn McKenzie wrote: Yeah, hif I had known that you wanted a function where you loop through your array twice, that would have done it. Bravo. Shawn: I don't see another way. You go through the array converting string to time (seconds), sort, and then convert back. You have to go through the array more than once. Cheers, tedd The other way, is the most likely ultra-fast solution I posted. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting times (SOLVED)
At 9:31 PM -0600 2/14/09, Shawn McKenzie wrote: Yeah, hif I had known that you wanted a function where you loop through your array twice, that would have done it. Bravo. Shawn: I don't see another way. You go through the array converting string to time (seconds), sort, and then convert back. You have to go through the array more than once. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting times (SOLVED)
tedd wrote: At 9:31 PM -0600 2/14/09, Shawn McKenzie wrote: Yeah, hif I had known that you wanted a function where you loop through your array twice, that would have done it. Bravo. Shawn: I don't see another way. You go through the array converting string to time (seconds), sort, and then convert back. You have to go through the array more than once. Cheers, tedd The other way, is the most likely ultra-fast solution I posted. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting times (SOLVED)
At 4:15 PM -0500 2/14/09, John Corry wrote: 1. convert the string representation of times to timestamps using strtotime() 2. sort the timestamps 3. display the timestamps as strings using date('format', timestamp) Would that work? John Corry email: jco...@gmail.com John: Bingo -- that worked! Thanks. tedd --- Here's the code. ?php // == returns a time array sorted function sortTime($in_times) { $time = array(); foreach ($in_times as $t) { $time [] = strtotime($t); } sort($time); $sort_time = array(); foreach ($time as $t) { $sort_time[] = date('g:ia', $t); } return $sort_time; } ? -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting times (SOLVED)
tedd wrote: At 4:15 PM -0500 2/14/09, John Corry wrote: 1. convert the string representation of times to timestamps using strtotime() 2. sort the timestamps 3. display the timestamps as strings using date('format', timestamp) Would that work? John Corry email: jco...@gmail.com John: Bingo -- that worked! Thanks. tedd --- Here's the code. ?php // == returns a time array sorted function sortTime($in_times) { $time = array(); foreach ($in_times as $t) { $time [] = strtotime($t); } sort($time); $sort_time = array(); foreach ($time as $t) { $sort_time[] = date('g:ia', $t); } return $sort_time; } ? Yeah, hif I had known that you wanted a function where you loop through your array twice, that would have done it. Bravo. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php