PHP是否支持正确的时间比较if-statement?

时间:2016-03-21 15:14:01

标签: php function datetime if-statement comparison

我想检查一下是否使用PHP工作。但是我无法使用任何日期函数进行适当的时间比较。 PHP是否支持使用日期函数进行时间比较?

目前我通过计算当天的分钟数量来解决我的问题:

<?php
    date_default_timezone_set('Europe/Amsterdam');
    $hours = date('G');
    $minutes = date('i');
    $totalMinutes = ($hours * 60) + $minutes;
    $betweenMinutes1 = (8 * 60) + 30;
    $betweenMinutes2 = (17 * 60) + 0;

    echo $hours.' * 60 + '.$minutes.' = '.$totalMinutes.' minuten van de 1440 op een dag verstreken <br/><br/>';

    echo 'if '.$totalMinutes.' is between '.$betweenMinutes1.' and '.$betweenMinutes2.' it is workingtime:';

    if (!($totalMinutes > $betweenMinutes1 && $totalMinutes < $betweenMinutes2)) {
        echo 'It isn\'t workingtime.';
    } else {
        echo 'It is workingtime.';
    }
?>

这是最好的方式,还是有办法在if语句中使用正确的日期函数

1 个答案:

答案 0 :(得分:0)

对于一个非常小的条件,这是非常长的代码;您只想检查当前时间是否在您给定的范围内

$current=date("Gi");   // hours without leading zeroes and minutes with.
if($current > 830 && $current < 1700)   // 0830 to 1700 hours
{
    // we're open 
}

要回答你的实际问题,是的PHP确实有时间比较功能。您可以找到一大堆 here