【问题标题】:How to specify saturday as a week day for strtotime如何将星期六指定为 strtotime 的工作日
【发布时间】:2014-01-17 13:14:48
【问题描述】:

我需要在当前日期上增加 2 个工作日。 我本来打算用strtotime,但是strtotime的工作日不包括星期六。

$now = date("Y-m-d H:i:s");
$add = 2;
$format = "d.m.Y";
if(date('H') < 12) {
    $add = 1;
}
$date = strtotime($now . ' +'.$add.' weekdays');
echo date($format, $date);

如果您在星期五运行它,它将在星期二输出。但它实际上应该在星期一返回。

如何将星期六添加为工作日?

【问题讨论】:

  • 我不认为你可以。如果您需要特定的业务逻辑,您应该自己实现它,而不是依赖strtotime 的自动魔法。 “工作日”的定义与“工作日”的定义不同。假期呢?

标签: php strtotime


【解决方案1】:

从特定日期获取下一个工作日+偏移天数:

function get_next_business_date($from, $days) {
    $workingDays = [1, 2, 3, 4, 5, 6]; # date format = N (1 = Monday, ...)
    $holidayDays = ['*-12-25', '*-01-01', '2013-12-24']; # variable and fixed holidays

    $from = new DateTime($from);
    while ($days) {
        $from->modify('+1 day');
        if (!in_array($from->format('N'), $workingDays)) continue;
        if (in_array($from->format('Y-m-d'), $holidayDays)) continue;
        if (in_array($from->format('*-m-d'), $holidayDays)) continue;
        $days--;
    }
    return $from->format('Y-m-d'); #  or just return DateTime object
}

print_r( get_next_business_date('today', 2) );

demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多