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 }
精彩评论