简体   繁体   English

检查是否超过两个小时

[英]Check if more than two hours has passed

I'm looking for an easy way to see if more than two hours has passed between two dates. 我正在寻找一种简单的方法来查看两个日期之间是否超过两个小时。 I can either do this with a MySQL DATETIME value, or if needed, I can convert that to a UNIX timestamp. 我可以使用MySQL DATETIME值执行此操作,或者如果需要,我可以将其转换为UNIX时间戳。 I just need an easy way to to compare those two dates and see if more than 2 hours has passed. 我只需要一个简单的方法来比较这两个日期,看看是否已超过2个小时。

尝试在MySQL中查看DATEDIFF函数。

A UNIX timestamp is just the number of seconds that have elapsed since 12:00AM UTC, January 1, 1970. UNIX时间戳只是自1970年1月1日UTC上午12:00起经过的秒数。

Two hours in seconds is 60 * 60 * 2 = 7200 . 两小时的秒数是60 * 60 * 2 = 7200 So, 所以,

 if($secondTimestamp - $firstTimestamp >= 7200)
 {
      echo '2 hours have elapsed.';
 }

Since you tagged with php, you could use PHP's DateTime::diff ( DateTime::diff ) to get a diff between two datetime objects. 由于您使用php标记,您可以使用PHP的DateTime :: diff( DateTime :: diff )来获取两个日期时间对象之间的差异。 I guess it depends on where in your application you are doing the comparison. 我想这取决于您在应用程序中进行比较的位置。

In PHP 在PHP中

  $time = strtotime($date2) - strtotime($date1); //this will give difference in seconds between two dates

  if(($time/3600) >= 2) { // 2 hours has left }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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