PHP 中的工作日(周一至周五)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4261179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:27:06  来源:igfitidea点击:

Working days (Mon-Fri) in PHP

phpstrtotime

提问by fredley

Is there a way to use strtotimeto add working days (Monday to Friday) to a date? Or some other method? What I want to do is:

有没有办法strtotime将工作日(周一至周五)添加到日期?或者其他什么方法?我想做的是:

date ( 'Y-m-j' , strtotime ( '+3 working days' ) )

回答by John Giotta

If you are limiting to weekdays use the string weekdays.

如果您仅限于工作日,请使用字符串 weekdays。

echo date ( 'Y-m-j' , strtotime ( '3 weekdays' ) );

This should jump you ahead by 3 weekdays, so if it is Thursday it will add the additional weekend time.

这应该会让你提前 3 个工作日,所以如果是星期四,它会增加额外的周末时间。

Source: http://www.php.net/manual/en/datetime.formats.relative.php

来源:http: //www.php.net/manual/en/datetime.formats.relative.php

回答by ghindows

For PHP >= 5.6

对于 PHP >= 5.6

public function addWorkingDays($date, $day)
{

    if (!($date instanceof \DateTime) || is_string($date)) {
        $date = new \DateTime($date);
    }

    if ($date instanceof \DateTime) {
        $newDate = clone $date;
    }

    if ($day == 0) {
        return $newDate;
    }

    $i = 1;

    while ($i <= abs($day)) {

        $newDate->modify(($day > 0 ? ' +' : ' -') . '1 day');

        $next_day_number = $newDate->format('N');

        if (!in_array($next_day_number, [6, 7])) {
            $i++;
        }

    }

    return $newDate;

}

回答by mpickens

I have found this buggy when needing a larger amount of weekdays. I was looking for X amount of business days after the 1st of the current month.

当需要大量工作日时,我发现了这个越野车。我正在寻找当月 1 日之后的 X 个工作日。

Looked great at first until after adding > 5 business days (similar to what @zerkms found).

一开始看起来很棒,直到添加 > 5 个工作日后(类似于@zerkms 的发现)。

This has proved more accurate for me.

事实证明,这对我来说更准确。

    function _getBusinessDayOfMonth( $days ) { 
        $time = strtotime(date("m/1/Y 00:00")); //finding # of business days after 1st of the month
        $i = 0; //start with zero
        while ($i < $days) { //loop through until reached the amount of weekdays
            $time = strtotime("+1 day", $time); //Increase day by 1
            if (date("N", $time) < 6) { //test if M-F
                $i++; //Increase by 1
            }
        }
        echo date("m/d/Y", $time);
    }

回答by CSelcukB

I think a function can be easily developed that you can just export current day no of the week and you can add two and mod of 5 will give you easily weekday.

我认为可以轻松开发一个功能,您可以只导出一周中的当前日期,您可以添加两个和 5 的 mod 将轻松为您提供工作日。

function increaseWorkDay($workDayToProcess, $dayToAdd){
    if($workDayToProcess >= 4 && $workDayToProcess <= 6){
        $workDayToProcess= 4;
    }
    $workDayToProcess+= $dayToAdd;

    return $workDayToProcess % 5;
}

And you can export the weekday name by using an array, this method can alternatively be used.

并且您可以使用数组导出工作日名称,也可以使用此方法。

回答by Pedro H. de Fran?a

I do it recursively, worked for me

我递归地做,为我工作

function add_work_days($date, $day){
    if($day == 0)
        return $date;

    $date->add(new DateInterval('P1D'));

    if(!in_array($date->format('N'), array('6', '7')))
        $day--;

    return add_work_days($date, $day);
}

$date  = add_work_days(new DateTime(), 3);
echo $date->format('d/m/Y');

回答by ram

For older versions of PHP < 5.3

对于旧版本的 PHP < 5.3

function AddWorkingDays($startDate, $adddays)
{
  $retdate = $startDate;
  $sign = "+";
  if($adddays < 0){
    $adddays = $adddays*-1;
    $sign = "-";
  }
  while ($adddays > 0) {
    $retdate = date ( 'Y-m-d' , strtotime ( "$retdate {$sign}1 day" ) );

      $what_day = date("N", strtotime($retdate));
      if ( $what_day != 6 && $what_day != 7 ) // 6 and 7 are weekend
          $adddays--;

  };

  return $retdate;

}