简体   繁体   中英

How to test if current time is bigger (later) than the time in a variable?

In a variable $time I have this :

Fri, 25 Apr 2014 22:20:54

This $time I would like to contrast it with the current date/time.

For example, if the $currentTime is equal to $time or less than $time + 5 minutes, then echo yes.

If I create the same format of date/time for the $currentTime (which I do not know how), will the if = || < help me ?

How an I do this?

DateTime() objects are comparable:

$date = DateTime::createFromFormat('D, d M Y H:i:s', 'Fri, 25 Apr 2014 22:20:54'); 
$now  = new DateTime(); 
if ($date > $now) {
    // it's in the future 
}

You can modify either DateTime object using DateTime::modify() if you want to add five minutes or any other amount of time.

$date   = DateTime::createFromFormat('D, d M Y H:i:s', 'Fri, 25 Apr 2014 22:20:54'); 
$now    = new DateTime(); 
$future = new DateTime('+5 minutes');
if ($date >= $now && $date <= $future) {
    // it's in the future but less then five minutes so
}

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