Answer the question
In order to leave comments, you need to log in
What is the best way to check if a specific time is within a given time range?
There is one wonderful TIME type in MySQL, which writes the time in the HH:MM:SS format. In my table there are 2 fields with type TIME - start and end of the time range. I need to use PHP to determine if a specific time, such as 12:45:00 pm, is within the required range.
How can this be done? As I understand it, converting time to a number via strtotime is not the best solution.
Answer the question
In order to leave comments, you need to log in
It is better to do this at the stage of sampling from the database.
After all, when you make a query to the database, you most likely already know the range.
If the question is not to select only those that fall into the range, but to select all, and those who are in the range "mark" - again, this can be done at the query level:
SELECT
IF(timefield >= TIME('14:51:08') AND timefield <= TIME('14:51:09'),1,0) as intime
FROM ....
You can split the string at the colon and compare in parts.
You can remove the colons and compare the result as numbers.
And you can not take a steam bath and just compare the strings.
According to the string comparison algorithm, everything will be exactly checked if you have a 24-hour format and hours / minutes / seconds are always set to a two-digit number.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question