有没有办法使用strtotime将工作日(星期一到星期五)添加到日期中?或者其他方法?我想做的是:
date ( 'Y-m-j' , strtotime ( '+3 working days' ) )发布于 2010-11-24 05:38:42
如果限制为工作日,请使用字符串weekdays。
echo date ( 'Y-m-j' , strtotime ( '3 weekdays' ) );这应该会让你提前3个工作日,所以如果是周四,就会增加额外的周末时间。
来源:http://www.php.net/manual/en/datetime.formats.relative.php
发布于 2013-06-29 03:05:13
当我需要更多的工作日时,我发现了这个问题。我正在寻找在本月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);
}发布于 2018-04-20 04:30:54
对于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;
}https://stackoverflow.com/questions/4261179
复制相似问题