Question

I have a MySQL table with hours:

10:30
11:00
9:00

how to format the numbers in PHP output and order them ASC?

I suppose, I need to change the format of the numbers to something like this?

09:00
10:30
11:00
Was it helpful?

Solution

$times_array = array();
$times = array("10:30","11:00","9:00");
foreach($times as $time){
    $times_array[strtotime($time)] = $time;
}

ksort($times_array);
print_r($times_array);

OTHER TIPS

i think you should change your field type for a "time" format. For ordering it's the best

Here's an idea...

Store the time (PHP time() function), that way you'll be able to do whatever you want easily.

$strTime = time();
INSERT INTO tablename (time) VALUES ('$time');

Then you'll be able to sort easily...

// For example - simple query
SELECT time FROM table ORDER BY time ASC

or

SELECT HOUR(time), MINUTE(time) FROM table ORDER BY HOUR(time), MINUTE(TIME) ASC;

Displaying in PHP...

$strTimeFromDB = $arrDBResult['time'];

// Displaying the time e.g.: 11:00
echo date("H:i", $strTimeFromDB);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top