Q: Trying to order an array by minutes remaining I have an array of data and I'm wanting to sort this array by the difference in time between the current time and a date stamp value I have stored for each item in the Array.
So for example, the time is now 14:47:57 and my array may have the following times:
23:00:00
00:30:00
19:00:00
20:30:00
00:00:00
When sorting I would want the array to be in the following order (with the nearest to the current time first - the time always has to go forward however)
19:00:00
20:30:00
23:00:00
00:00:00
00:30:00
Any advice of what I need to be looking at? I've looked at TIMESTAMPDIFF when getting the values out of the database but the results I'm getting don't match. 
I can also retrieve the difference in minutes between the current time and item array time and sort it based on that, but wondered if there was a 'cleaner' way. 
I'm using codeigniter a well, if anybody know of any helpers that can simplify this.
Any help appreciated, cheers.

A: I would suggest you to use usort:
$time_now = new DateTime('...');
$times=[...];

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return 
       ($today->diff(new DateTime($a)) < $today->diff(new DateTime($b)))
           ? -1 : 1;
}

usort($times, "cmp");

Hope it helps.

A: If you have an array of strings, maybe you could sort your array first, and then try to rearrange it according to the index... Something like this?
<?
$times = array("23:00:00","00:30:00","19:00:00","20:30:00","00:00:00");
$times = sortByTime($times);

$nextTimes = getNextTimes($times,"14:47:57");
var_dump($nextTimes);

function sortByTime($array)
{
        $ret = array();
        foreach($array as $time)
        {
                $ret[strtotime($time)] = $time;
        }
        ksort($ret);
        return $ret;
}

function getNextTimes($array,$hour)
{
        $timestamp = strtotime($hour);
        $index = 0;
        foreach($array as $key=>$val)
        {
                if($timestamp < $key)
                {
                        break;
                }
                $index++;
        }
        return array_merge(array_splice($array,$index),array_splice($array,0,$index));
}
?>

