简报:
我正在尝试计算我正在构建的自定义项目管理系统的两个日期之间的工作日。这是我到目前为止所做的,如下所示。它似乎工作正常,但我不确定它有多准确以及我是否应该使用它。任何反馈将不胜感激!
代码
<?php
# date variables:
date_default_timezone_set('Asia/Kuwait');
$date['start'] = date('Y-m-d H:i:s');
$date['end'] = '2013-12-01 08:00:00';
$date['off'] = array('Friday','Saturday'); # usual days off in Kuwait)
# calculate the difference:
$date_s = new DateTime($date['start']);
$date_e = new DateTime($date['end']);
$interval = $date_s->diff($date_e);
# obtain relevant values:
$remaining_days = $interval->format('%r%a');
$remaining_weeks = floor($remaining_days/7);
$weekend_days = ($remaining_weeks*count($date['off']));
# additional holidays (just an example):
$extra_holidays_array = array
(
'holiday 1' => '2013-06-24 00:00:00',
'holiday 2' => '2013-06-25 00:00:00',
'holiday 3' => '2013-07-01 00:00:00',
'holiday 4' => '2013-08-24 00:00:00'
);
# check if a real holiday:
$extra_holidays = 0;
foreach( $extra_holidays_array as $check_date )
{
$day_of_holiday = date('l', strtotime($check_date));
if( ! in_array($day_of_holiday,$date['off']) ){ $extra_holidays++; }
}
# total holidays:
$total_holidays = ($weekend_days+$extra_holidays);
# business days:
$business_days_nh = ($remaining_days-$weekend_days); # NO extra holidays
$business_days_wh = ($remaining_days-$weekend_days-$extra_holidays); # WITH extra holidays
?>
<ul>
<li>Current Date: <?php echo $date['start']; ?></li>
<li>Deadline Date: <?php echo $date['end']; ?></li>
</ul>
<ul>
<li>Remaining Days: <?php echo $remaining_days; ?></li>
<li>Remaining Weeks: <?php echo $remaining_weeks; ?></li>
</ul>
<ul>
<li>Usual Holidays: <?php echo $weekend_days; ?></li>
<li>Extra Holidays: <?php echo $extra_holidays; ?></li>
<li>Total Holidays: <?php echo $total_holidays; ?></li>
</ul>
<ul>
<li>Business Days (Before Holidays): <?php echo $business_days_nh; ?></li>
<li>Business Days (After Holidays): <?php echo $business_days_wh; ?></li>
</ul>