1

目前我正在做类似以下的事情

if(date("H") > 23) {
    if(date("H") < 24) {
        if(date("i") < 10) {
            if(date("i") < 20) {
                //perform an action if it's between 11:10pm and 11:20pm
            }
        }
    }
}

if执行此类语句的更快和更清洁的方法是什么?

4

2 回答 2

3

代码并没有少很多,但可能更容易阅读:

$now   = new DateTime();
$early = new DateTime('23:10');
$late  = new DateTime('23:20');
if ($now > $early && $now < $late) {
    //perform an action if it's between 11:10pm and 11:20pm
} 
于 2014-06-13T13:33:29.147 回答
0

有很多方法可以做到这一点。这是一个。

$now = date('H:i');
$target1 = date('23:10');
$target2 = date('23:20');
if ($now > $target1 && $now < $target2)
{
     // Do something
}
于 2014-06-13T13:34:20.197 回答