周一至周五的09:00至17:30之间的工作日

时间:2014-06-09 11:49:30

标签: php date time

嘿我正在创建一个函数,它返回true或false,具体取决于它是否在工作时间。

/**
 * check if it is between 9-5 on a workday
 * @return bool
 */
private function currentlyWorkHours()
{
    $start = "09:00:00";
    $end = "17:30:00";
    $today = date("F j, Y, g:i a");

    if(!$this->isWeekend($today))
    {
        if($today > $start && $today < $end)
        {
            return true;
        }
    }

    return false;
}

private function isWeekend($date)
{
    return (date('N', strtotime($date)) >= 6);
}

这是一种很好的方法,还是有更好的替代方案?

由于

2 个答案:

答案 0 :(得分:1)

只需将当前日期/时间分为星期和时间,然后执行比较,如下所示:

list($dow, $time) = explode(' ', date('w H:i:s'), 2);

return $dow >= 1 && $dow <= 5 && $time >= $start && $time <= $end;

答案 1 :(得分:0)

/**
 * check if it is between 9-5 on a workday
 * @return bool
 */
private function currentlyWorkHours()
{
    $start = "09:00:00";
    $end = "17:30:00";
    $today = date("F j, Y, g:i a");
    $todayTime = date('H:i:s');

    if(!$this->isWeekend($today))
    {
        if($todayTime > $start && $todayTime < $end)
        {
            return true;
        }
    }

    return false;
}

private function isWeekend($date)
{
    return (date('N', strtotime($date)) >= 6);
}

$today更改为date('H:i:s')