0
我試圖計算兩個日期自定義項目管理系統我建立的工作日。以下是我所做的,如下所示。它似乎工作正常,但我不知道它有多準確,是否應該使用它。任何反饋將不勝感激!
CODE
<?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>
您是否有一套覆蓋邊緣案例的單元測試? –
對不起,我不明白。你究竟是什麼意思?如果我所假設的是正確的,那麼如果開始日期等於或大於結束日期,我會得到負值,這對我來說不是問題。 –
我的意思是,許多開發人員還編寫單元測試以使用代碼,測試它們可以運行以證明代碼正常工作。這樣,他們不需要詢問其他人是否有效,但如果所有測試都通過,就可以證明它是有效的。 –