0

Say you have a list of time values in a column in a MySQL database.

Time
-----
10:15 AM
10:35 AM
10:55 AM

And say that right now is 10:27 AM. How would I get the 10:35 AM value specifically in a variable in PHP?

I know that I could get the current time with

$currenttime = time();

and then operate on the time values with

$some_time = strtotime($row['time']);

But how do I get the closest one into that $some_time variable?

3 Answers 3

1

You want the row with the minimum delta between the recorded time and the reference time where the recorded time is greater than the reference time.

SELECT * FROM SomeTable AS T
 WHERE T.Time = (SELECT MIN(T2.Time) FROM SomeTable AS T2
                  WHERE T2.Time > ?)

The '?' is a placeholder for your reference time.

6
  • what's sometable and time represent? can you mix your code with my $variable settings so i can understand what you mean a bit more?
    – Dan
    Jan 31, 2011 at 12:40
  • @Dan: I presume your table has a name? You didn't tell us what your table was called (a remarkably common failing in SQL-related questions), so I had to invent a name for it; I chose 'SomeTable' to indicate that it was the name of some table in your database. I assume that the column name is 'Time'. You could replace my question mark placeholder with your $currenttime variable if you want to; you'd have to worry about the relevant quoting conventions. If you can use prepared statements with placeholders, it is simpler to pass the value at the point where you execute the query. Jan 31, 2011 at 14:53
  • yeah sorry about not being specific, it's late here um so if i wanted to pass the $currenttime variable instead of the question mark would I just do . "$currentime" .) or?
    – Dan
    Jan 31, 2011 at 16:20
  • also, this would only give the ONE closest value after the current time right? not an entire list?
    – Dan
    Jan 31, 2011 at 16:34
  • @Dan: something along the lines of: $sql = "SELECT * FROM SomeTable AS T1 WHERE T1.Time = (SELECT MIN(T2.Time) FROM SomeTable WHERE T2.Time > '$currenttime')";, but you'll need to worry about the data types and formatting of the data in the column and the value in $currenttime. Jan 31, 2011 at 16:36
1

You can get closest time from within your query using TIMESTAMPDIFF:

SELECT
  time, TIMESTAMPDIFF(MINUTE, time, NOW()) AS minutes
ORDER BY
  minutes, time DESC

If you want to select just one tow with closest time:

SELECT
  time, TIMESTAMPDIFF(MINUTE, time, NOW()) AS minutes
ORDER BY
  minutes, time DESC
LIMIT 1
0

Try taking the absolute values of the differences between the current time and possible times. Whichever value is lowest is the closest. The only thing from there you would have to worry about is if a given time is equally close to two times which you can handle in whichever way you please (it seems most reasonable to me to simply round up at that point)

Regards,
Dennis M.

1
  • Absolute times will sometimes select a time before the current (or reference) time. You need to ensure that the time is after the reference time, and then a simple minimum is sufficient. Jan 31, 2011 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.