Pick Out The Earliest Time Value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Pick Out The Earliest Time Value

Post 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
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Pick Out The Earliest Time Value

Post 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.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Pick Out The Earliest Time Value

Post 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
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Pick Out The Earliest Time Value

Post 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 :lol:
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: Pick Out The Earliest Time Value

Post by incubi »

Thanks for the help, that's a good start.

incubi
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Pick Out The Earliest Time Value

Post 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
?>
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Pick Out The Earliest Time Value

Post by McInfo »

1. With SQL:

Code: Select all

SELECT MIN(`field`) FROM `table`
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.
Post Reply