Check MySQL Comparison Time with DATETIME Printing

I want to check if it has passed an hour since the last mark datetimein MySQL. How to do it using PHP?

+3
source share
6 answers

The cleanest way to do this is in MySQL using DATE_SUB (or DATE_ADD depending on your goals). For instance:

SELECT DATE_SUB(NOW(), INTERVAL 1 HOUR)

Similarly, you can replace “1 HOUR” with other things, such as “1 DAY” or “1 MINUTE,” etc. I always prefer to make date comparisons at the database level, since presumably the dates are stored in a common format and clockwise offset.

+2
source

you could and probably should do it all in mysql instead

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

datetime .

+3
$db_time = strtotime($row->timestamp_col);
$age = 60 * 60;
if ($db_time < (time() - $age)) {
    doSomething();
}

This is not necessarily broken so you can see how it works.

0
source
$now = time();

$dbDate = strtotime($row['datetime']);

if ($now > $dbDate + 3600) {
   // yes
}
0
source

You put a check in the request. Then you can use MySQL date comparison, and PHP does not need to know or care.

0
source

Source: https://habr.com/ru/post/1783326/


All Articles