Php 检查是否有东西在时间之前到达

Php 检查是否有东西在时间之前到达,php,time,Php,Time,我想做一些事情,比如: $time = time(); //Store the time in the dabase //Some time later, say three hours this code runs // so if your time() was 2pm its now 5pm when this statement // is run. if($time < 4 hours){ // do something. } $time=time(); //将时间存

我想做一些事情,比如:

$time = time();
//Store the time in the dabase

//Some time later, say three hours this code runs
// so if your time() was 2pm its now 5pm when this statement
// is run.
if($time < 4 hours){
    // do something.
}
$time=time();
//将时间存储在数据库中
//一段时间后,假设代码运行了三个小时
//如果你的时间()是下午2点,那么现在是下午5点
//他跑了。
如果($时间<4小时){
//做点什么。
}
但我不确定最干净的方法是什么。

OOP风格

$start = new DateTime;

// Do something

if ($start < new DateTime('-4 hours')) {
    // Do something different
}
$start=新日期时间;
//做点什么
如果($start<新日期时间('-4小时')){
//做些不同的事情
}

非面向对象的方法也很简单

$start = time();

// Do something

if ($start < strtotime('-4 hours')) {
    // Do something different
}
$start=time();
//做点什么
如果($start

OOP风格

$start = new DateTime;

// Do something

if ($start < new DateTime('-4 hours')) {
    // Do something different
}
$start=新日期时间;
//做点什么
如果($start<新日期时间('-4小时')){
//做些不同的事情
}

非面向对象的方法也很简单

$start = time();

// Do something

if ($start < strtotime('-4 hours')) {
    // Do something different
}
$start=time();
//做点什么
如果($start
time()将以秒为单位返回php时间。当第二个代码块运行时,您希望再次检查time(),因此应执行以下操作:

$timeNow = time();
if($savedTime < $timeNow-14400){
    // do something
}
$timeNow=time();
如果($savedTime<$timeNow-14400){
//做点什么
}
其中14400为4小时(60*60*4==14400),单位为秒。当然,可能没有理由将time()设置为变量,只是以防万一。

time()将以秒为单位返回php时间。当第二个代码块运行时,您希望再次检查time(),因此应执行以下操作:

$timeNow = time();
if($savedTime < $timeNow-14400){
    // do something
}
$timeNow=time();
如果($savedTime<$timeNow-14400){
//做点什么
}

其中14400为4小时(60*60*4==14400),单位为秒。当然,可能没有理由将time()设置为变量,只是以防万一。

4小时后?离罗马帝国灭亡还有4个小时?凌晨4点之前?您的问题需要更多的上下文。首先,您需要为
hours
赋值,如
$hours=“”$time
超过4小时,您的意思是从现在起4小时?离罗马帝国灭亡还有4个小时?凌晨4点之前?您的问题需要更多的上下文。首先,您需要为
hours
赋值,如
$hours=“”$time
相比超过4小时,您的意思是什么?