C
C
Crash2018-02-21 12:42:19
PHP
Crash, 2018-02-21 12:42:19

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

2 answer(s)
A
Alex McArrow, 2018-02-21
@Bandicoot

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 ....

A
Anton fon Faust, 2018-02-21
@bubandos

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 question

Ask a Question

731 491 924 answers to any question