Re: Sorting times (SOLVED before tedds crappy SOLVED)
| From: | Shawn McKenzie | Date: | Mon, 16 Feb 2009 01:07:56 +0000 |
| Subject: | Re: Sorting times (SOLVED before tedds crappy SOLVED) | ||
| References: | 1 2 3 | Groups: | php.general php.general |
| Request: | Send a blank email to php-general+get-288295@lists.php.net to get a copy of this message | ||
Shawn McKenzie wrote:
> Shawn McKenzie wrote:
>> tedd wrote:
>>> Hi gang:
>>>
>>> Anyone have/know a routine that will sort an array of times?
>>>
>>> For example, a function that would take an array like this:
>>>
>>> time[0] ~ '1:30pm'
>>> time[1] ~ '7:30am'
>>> time[2] ~ '12:30pm'
>>>
>>> and order it to:
>>>
>>> time[0] ~ '7:30am'
>>> time[1] ~ '12:30pm'
>>> time[2] ~ '1:30pm'
>>>
>>>
>>> Cheers,
>>>
>>> tedd
>>>
>>>
>> Not tested:
>>
>> function time_sort($a, $b)
>> {
>> if (strtotime($a) == strtotime($b)) {
>> return 0;
>> }
>> return (strtotime($a) < strtotime($b) ? -1 : 1;
>> }
>>
>> usort($time, "time_sort");
>>
> Well, I just thought, since the strtotime() uses the current timestamp
> to calculate the new timestamp, if you only give it a time then the
> returned timestamp is today's date with the new time you passed. If you
> had a large array and the callback started at 23:59:59 then you could
> end up with some times from the date it started and some from the next
> day, which of course would not be sorted correctly with respect to times
> only. So, this might be better (not tested):
>
>
> 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;
> }
>
>
Your best bet above.
--
Thanks!
-Shawn
http://www.spidean.com