Page 1 of 1
Pick Out The Earliest Time Value
Posted: Thu Feb 18, 2010 8:17 am
by incubi
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
Re: Pick Out The Earliest Time Value
Posted: Thu Feb 18, 2010 8:32 am
by lshaw
Just select the result you want:
Code: Select all
mysql_query("SELECT * FROM `table` ORDER BY `time` LIMIT 1,1");
This puts the lowest time first and only selects that one.
Re: Pick Out The Earliest Time Value
Posted: Thu Feb 18, 2010 8:56 am
by incubi
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
Re: Pick Out The Earliest Time Value
Posted: Thu Feb 18, 2010 9:11 am
by lshaw
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:
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
This is just an untested attempt so please dont expect it to be perfect

Re: Pick Out The Earliest Time Value
Posted: Thu Feb 18, 2010 11:56 am
by incubi
Thanks for the help, that's a good start.
incubi
Re: Pick Out The Earliest Time Value
Posted: Thu Feb 18, 2010 1:19 pm
by lshaw
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
Posted: Thu Feb 18, 2010 11:00 pm
by McInfo
1. With SQL:
2. With PHP:
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
Edit: This post was recovered from search engine cache.