Does anyone know how to pick out the earliest time in an SQL query or an array of times?
For example, say this in an array or the result of a query
11:30:00
09:04:00
08:20:00
08:20:00
02:32:00
05:00:00
07:10:00
How would I pick out 02:32:00 from it?
Thank you
incubi
Pick Out The Earliest Time Value
Moderator: General Moderators
Re: Pick Out The Earliest Time Value
Just select the result you want:
This puts the lowest time first and only selects that one.
Code: Select all
mysql_query("SELECT * FROM `table` ORDER BY `time` LIMIT 1,1");
Re: Pick Out The Earliest Time Value
I think that will work, thank you. Just for learning sake is there a way to do the same thing in a conditional statement, for example if the values were in an array? I guess there would be some math on the time values to do it, right?
Thanks
incubi
Thanks
incubi
Re: Pick Out The Earliest Time Value
I am not too good with arrays, but you can sort an array using sort($array)
You may the be able to use code like this:
This is just an untested attempt so please dont expect it to be perfect 
You may the be able to use code like this:
Code: Select all
$array=mysql_fetch_array($query);
sort($array);
$lowest_date=$array['date'][0]; //the first value will be the lowest as they are in ascending order
Re: Pick Out The Earliest Time Value
Thanks for the help, that's a good start.
incubi
incubi
Re: Pick Out The Earliest Time Value
I have found some code that work for sorting an assoc array, and tested it (not using a database)
Code: Select all
<?php
$array=array();
$array["date"][0]=7;
$array["date"][1]=2;//this number will be shown as it is lower
sort($array["date"]); //sort the array on the date key
$lowest_date=$array['date'][0]; //the first value will be the lowest as they are in ascending order
echo $lowest_date; //will show 2
?>
Re: Pick Out The Earliest Time Value
1. With SQL:
2. With PHP:
Edit: This post was recovered from search engine cache.
Code: Select all
SELECT MIN(`field`) FROM `table`Code: Select all
<?php
$times = array('11:30:00', '09:04:00', '08:20:00', '08:20:00', '02:32:00', '05:00:00', '07:10:00');
echo min($times); // 02:32:00