开发者

Check if more than two hours has passed

开发者 https://www.devze.com 2023-03-24 06:05 出处:网络
I\'m look开发者_StackOverflow中文版ing 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

I'm look开发者_StackOverflow中文版ing 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. I just need an easy way to to compare those two dates and see if more than 2 hours has passed.


try to look into DATEDIFF function in MySQL.


A UNIX timestamp is just the number of seconds that have elapsed since 12:00AM UTC, January 1, 1970.

Two hours in seconds is 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. I guess it depends on where in your application you are doing the comparison.


In PHP

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

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

精彩评论

暂无评论...
验证码 换一张
取 消