鉴于$some_date
等于2015-02-12 10:28:04
,如何使用PHP(而非MySQL)确定它是否不比当前时间的X
小?
答案 0 :(得分:2)
strtotime
和DateTime
课程是我们的朋友。 $x
小时为:
if(($time = strtotime($some_date)) > $time + ($x * 360)) {
//do something its more than X hours
}
这可能不适用于夏令时间界限,所以可能:
if(($time = strtotime($some_date)) > strtotime("+$x hours", $time)) {
//do something its more than X hours
}
答案 1 :(得分:1)
这将确定您的日期是否距离当前日期时间不到2小时
<?php
$date = new DateTime(); //set current datetime to variable
$date->setTimezone(new DateTimeZone('America/Detroit')); //set timezone if you like
$fdate = $date->format('Y-m-d H:i:s'); //change format to year - month - day, hour, minute, seconds
$some_date = strtotime('2015-02-13 18:30:04'); // this is your datetime. use your time or change $some_date to your variable
$new_date = strtotime($fdate) - $some_date;
$minute_date = $new_date / 60; // convert to minutes
$hour_date = $minute_date / 60; // convert to hours
print $hour_date;
if($hour_date > 2){ // change the 2 to whatever you want it to be.
print "more than two hours";
}else{
print "less than two hours";
}
?>