php - 如何将周六指定为 strtotime 的工作日

标签 php strtotime

我需要在当前日期的基础上添加 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);

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

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

最佳答案

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

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

关于php - 如何将周六指定为 strtotime 的工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21186962/

相关文章:

php - 生成唯一的数组值

php - 如何使用 Laravel 根据特定列选择 unique() 模型?

php - 什么是 Unix 时间戳以及为什么要使用它?

PHP - 当周从星期一以外的一天开始时,获取给定周数的第一天

php - 我如何检查日期与当前日期

php, date() 没有返回正确的日期/时间

php - 带有来自另一个表的字段检查的mysql DELETE

javascript - 如何将 PHP 行记录插入到 AJAX url 中

php - 如何在 PHP 中对数组和数据进行排序?

PHP - 以秒为单位获取两个日期时间之间的差异