简体   繁体   中英

How can I check if the time is more than an hour later?

So I have a database entry that update the date/time in yyyy-mm-dd hh:mm:ss .

Now I want to check, if there is a space inbetween the the database and the actual time from 60 minutes. How can I do that?

Example:

DB: 2020-02-14 10:00:00
Time now: 2020-02-14 11:01:00
Do something

DB: 2020-02-14 10:00:00
Time now: 2020-02-14 10:59:00
Do nothing

You can use something like this:

$t1 = strtotime( '2006-04-14 11:30:00' );
$t2 = strtotime( '2006-04-12 12:30:00' );
$diff = round(($t1 - $t2) / 3600);

In MySQL, you can do date arithmetics:

update mytable
set mydatetime = now()
where mydatetime <= now() - interval 1 hour and id = ?

The where clause filters on record whose column mydatetime is more than one hour old. You did not tell what you want to do , so I assumed an update query, that resets the date/time to the current date/time.

This assumes that you want to update the timestamp of a given record , not accross the whole table, hence condition id = ? , which you can adapt - or remove - for your use case.

Hello Schmaniel at first i think you should use Carbon() https://carbon.nesbot.com/docs/ to get the right results. It's a great way to work with timeformats and timestamps.

 $now = Carbon::now(); $dbtime = Carbon::createFromFormat('Ymd H:i:s', '1975-05-21 22:10:22'); $totalDuration = $now->diffForHumans($dbtime); dd($totalDuration);

$currentDate = new DateTime();
$databaseDate = new DateTime($dateFromDatabase);
$interval = $datetime1->diff($datetime2);

Then you can check using the $interval variable

You can use mysql TIMESTAMPDIFF like this:

SELECT TIMESTAMPDIFF(HOUR,'2003-02-01','2003-05-01 12:05:55');

you can use one of the follwing units:

MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM