Php 在当前日期基础上增加 5 个工作日,不包括周末(周六至周日)和(多个)假期
Php add 5 working days to current date excluding weekends (sat-sun) and excluding (multiple) holidays
对于我们的网店交付,我们需要从 php 中的当前日期算起 5 个工作日。
我们的工作日是周一到周五,还有几个休息日(节假日)也不能算进去。
我找到了这个脚本,但这不包括假期。
<?php
$_POST['startdate'] = date("Y-m-d");
$_POST['numberofdays'] = 5;
$d = new DateTime( $_POST['startdate'] );
$t = $d->getTimestamp();
// loop for X days
for($i=0; $i<$_POST['numberofdays']; $i++){
// add 1 day to timestamp
$addDay = 86400;
// get what day it is next day
$nextDay = date('w', ($t+$addDay));
// if it's Saturday or Sunday get $i-1
if($nextDay == 0 || $nextDay == 6) {
$i--;
}
// modify timestamp, add 1 day
$t = $t+$addDay;
}
$d->setTimestamp($t);
echo $d->format('Y-m-d'). "\n";
?>
您可以使用 "while statement",循环直到获得足够的 5 天。每次循环获取并检查第二天是否在假期列表中。
示例如下:
$holidayDates = array(
'2016-03-26',
'2016-03-27',
'2016-03-28',
'2016-03-29',
'2016-04-05',
);
$count5WD = 0;
$temp = strtotime("2016-03-25 00:00:00"); //example as today is 2016-03-25
while($count5WD<5){
$next1WD = strtotime('+1 weekday', $temp);
$next1WDDate = date('Y-m-d', $next1WD);
if(!in_array($next1WDDate, $holidayDates)){
$count5WD++;
}
$temp = $next1WD;
}
$next5WD = date("Y-m-d", $temp);
echo $next5WD; //if today is 2016-03-25 then it will return 2016-04-06 as many days between are holidays
基于 Tinh Dang 回答的函数:
function getFutureBusinessDay($num_business_days, $today_ymd = null, $holiday_dates_ymd = []) {
$num_business_days = min($num_business_days, 1000);
$business_day_count = 0;
$current_timestamp = empty($today_ymd) ? time() : strtotime($today_ymd);
while ($business_day_count < $num_business_days) {
$next1WD = strtotime('+1 weekday', $current_timestamp);
$next1WDDate = date('Y-m-d', $next1WD);
if (!in_array($next1WDDate, $holiday_dates_ymd)) {
$business_day_count++;
}
$current_timestamp = $next1WD;
}
return date('Y-m-d', $current_timestamp);
}
我将循环限制为 1000 个工作日。如果需要可以没有限制。
根据卢克的回答:
不同的是这个每年都会生成假期
<?php
class DateHelper
{
//change at will
const HOLIDAY_DATES = [
['day' => 25, 'month' => 12],//christimas
['day' => 1, 'month' => 1],//new year
['day' => 13, 'month' => 4]//easter
];
/**
* @param int $numBusinessDays
* @param \DateTimeInterface $date
* @return \DateTime
*/
public static function getFutureBusinessDay(int $numBusinessDays, \DateTimeInterface $date)
{
$numBusinessDays = min($numBusinessDays, 1000);
$businessDayCount = 0;
$currentTimestamp = strtotime($date->format('Y-m-d'));
$holidayDates = self::getHolidayDates();
while ($businessDayCount < $numBusinessDays) {
$next1WD = strtotime('+1 weekday', $currentTimestamp);
$next1WDDate = date('Y-m-d', $next1WD);
if (!in_array($next1WDDate, $holidayDates)) {
$businessDayCount++;
}
$currentTimestamp = $next1WD;
}
return (new \DateTime())->setTimestamp($currentTimestamp);
}
/**
* @return array
*/
private static function getHolidayDates()
{
$holidays = [];
foreach (self::HOLIDAY_DATES as $holidayDate) {
$date = new \DateTime();
$date->setDate($date->format('Y'), $holidayDate['month'], $holidayDate['day']);
$holidays[] = $date->format('Y-m-d');
}
return $holidays;
}
}
对于我们的网店交付,我们需要从 php 中的当前日期算起 5 个工作日。
我们的工作日是周一到周五,还有几个休息日(节假日)也不能算进去。
我找到了这个脚本,但这不包括假期。
<?php
$_POST['startdate'] = date("Y-m-d");
$_POST['numberofdays'] = 5;
$d = new DateTime( $_POST['startdate'] );
$t = $d->getTimestamp();
// loop for X days
for($i=0; $i<$_POST['numberofdays']; $i++){
// add 1 day to timestamp
$addDay = 86400;
// get what day it is next day
$nextDay = date('w', ($t+$addDay));
// if it's Saturday or Sunday get $i-1
if($nextDay == 0 || $nextDay == 6) {
$i--;
}
// modify timestamp, add 1 day
$t = $t+$addDay;
}
$d->setTimestamp($t);
echo $d->format('Y-m-d'). "\n";
?>
您可以使用 "while statement",循环直到获得足够的 5 天。每次循环获取并检查第二天是否在假期列表中。
示例如下:
$holidayDates = array(
'2016-03-26',
'2016-03-27',
'2016-03-28',
'2016-03-29',
'2016-04-05',
);
$count5WD = 0;
$temp = strtotime("2016-03-25 00:00:00"); //example as today is 2016-03-25
while($count5WD<5){
$next1WD = strtotime('+1 weekday', $temp);
$next1WDDate = date('Y-m-d', $next1WD);
if(!in_array($next1WDDate, $holidayDates)){
$count5WD++;
}
$temp = $next1WD;
}
$next5WD = date("Y-m-d", $temp);
echo $next5WD; //if today is 2016-03-25 then it will return 2016-04-06 as many days between are holidays
基于 Tinh Dang 回答的函数:
function getFutureBusinessDay($num_business_days, $today_ymd = null, $holiday_dates_ymd = []) {
$num_business_days = min($num_business_days, 1000);
$business_day_count = 0;
$current_timestamp = empty($today_ymd) ? time() : strtotime($today_ymd);
while ($business_day_count < $num_business_days) {
$next1WD = strtotime('+1 weekday', $current_timestamp);
$next1WDDate = date('Y-m-d', $next1WD);
if (!in_array($next1WDDate, $holiday_dates_ymd)) {
$business_day_count++;
}
$current_timestamp = $next1WD;
}
return date('Y-m-d', $current_timestamp);
}
我将循环限制为 1000 个工作日。如果需要可以没有限制。
根据卢克的回答:
不同的是这个每年都会生成假期
<?php
class DateHelper
{
//change at will
const HOLIDAY_DATES = [
['day' => 25, 'month' => 12],//christimas
['day' => 1, 'month' => 1],//new year
['day' => 13, 'month' => 4]//easter
];
/**
* @param int $numBusinessDays
* @param \DateTimeInterface $date
* @return \DateTime
*/
public static function getFutureBusinessDay(int $numBusinessDays, \DateTimeInterface $date)
{
$numBusinessDays = min($numBusinessDays, 1000);
$businessDayCount = 0;
$currentTimestamp = strtotime($date->format('Y-m-d'));
$holidayDates = self::getHolidayDates();
while ($businessDayCount < $numBusinessDays) {
$next1WD = strtotime('+1 weekday', $currentTimestamp);
$next1WDDate = date('Y-m-d', $next1WD);
if (!in_array($next1WDDate, $holidayDates)) {
$businessDayCount++;
}
$currentTimestamp = $next1WD;
}
return (new \DateTime())->setTimestamp($currentTimestamp);
}
/**
* @return array
*/
private static function getHolidayDates()
{
$holidays = [];
foreach (self::HOLIDAY_DATES as $holidayDate) {
$date = new \DateTime();
$date->setDate($date->format('Y'), $holidayDate['month'], $holidayDate['day']);
$holidays[] = $date->format('Y-m-d');
}
return $holidays;
}
}