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

发布于 2024-10-03 17:09:42 字数 145 浏览 6 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

只是我以为 2024-10-10 17:09:42

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

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

这将使您提前 3 个工作日,因此如果是星期四,则会增加额外的周末时间。

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

If you are limiting to weekdays use the string 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.

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

薄荷→糖丶微凉 2024-10-10 17:09:42

您也可以这样做:

echo date_create()->modify("+3 weekdays")->format("Y-m-d");

请参阅:

https://www.php。 net/manual/en/function.date-create.php

https ://www.php.net/manual/en/datetime.modify.php

You can do this too:

echo date_create()->modify("+3 weekdays")->format("Y-m-d");

See:

https://www.php.net/manual/en/function.date-create.php

https://www.php.net/manual/en/datetime.modify.php

暮光沉寂 2024-10-10 17:09:42

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

起初看起来很棒,直到添加>之后5 个工作日(与@zerkms 发现的类似)。

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

    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);
    }

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.

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

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);
    }
一影成城 2024-10-10 17:09:42

对于 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;

}

For 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;

}
杯别 2024-10-10 17:09:42

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

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

    return $workDayToProcess % 5;
}

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

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.

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.

囍笑 2024-10-10 17:09:42

我递归地做,为我工作

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');

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');
浅忆 2024-10-10 17:09:42

对于旧版本的 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;

}

For older versions of 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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文