Schmaniel
Schmaniel

Reputation: 105

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

Upvotes: 1

Views: 1431

Answers (5)

larabee
larabee

Reputation: 156

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('Y-m-d H:i:s', '1975-05-21 22:10:22');

$totalDuration = $now->diffForHumans($dbtime);
dd($totalDuration);

Upvotes: 1

ROOT
ROOT

Reputation: 11622

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.

Upvotes: 0

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

Then you can check using the $interval variable

Upvotes: 0

Johnny Fagundes
Johnny Fagundes

Reputation: 68

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);

Upvotes: 2

GMB
GMB

Reputation: 222652

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

Upvotes: 1

Related Questions