4 回答

TA貢獻1805條經驗 獲得超9個贊
你不需要任何復雜的東西。您可以有一個簡單的 while 循環并計算工作日。這可能不是最有效的方法,但它應該有效。
function getNoOfWeekdays(string $startdate, string $enddate, string $format = 'd/m/Y'): int
{
$start = date_create_from_format($format, $startdate);
$end = date_create_from_format($format, $enddate);
$count = 0;
while ($start <= $end) {
if ($start->format('N') < 6) $count++;
$start->modify('+1 days');
}
return $count;
}

TA貢獻1789條經驗 獲得超8個贊
我為DateTime API創建了一個名為dt的PHP擴展。你可以在這里找到它。使用它非常簡單:
$start = '2019-10-01';
$end = '2019-11-01';
$start = dt::create($start);
//exclude end with true as 3.parameter
$countWeekdaysOct2019 = $start->countDaysTo('1,2,3,4,5',$end, true); //23
$countSundaysOct2019 = $start->countDaysTo('0',$end, true); //4
$countSaturdaysOct2019 = $start->countDaysTo('6',$end, true); //4

TA貢獻1844條經驗 獲得超8個贊
試試這個(見我上面的評論),以避免從夏天到冬天的問題:
$first = date('U', mktime(12 /* 12 pm!! */, 0, 0, $s[1], $s[0], $s[2]));
//
// some code here that the author didn't show and we hardly can extrapolate
// ...
//
$wk_days = array(1, 2, 3, 4, 5);
$days = 0;
for ($i = 0; $i < $total_days; $i++) {
$tmp = $first + ($i * 86400);
echo $i." x86400 add to ". $first . " ==> " . $tmp. " and the new date is " .date('Y-m-d',$tmp) ."<br/>";
$chk = date('w', $tmp);
if (in_array($chk, $wk_days)) {
$days++;
}
}

TA貢獻1895條經驗 獲得超7個贊
也許你可以試試這個代碼:
$initial = "2019-10-01";
$last = "2019-10-31";
$wk_days = array(1, 2, 3, 4, 5);
$days = 0;
$date = $initial;
do{
$chk = date("w",strtotime($date));
if(in_array($chk,$wk_days))
$days++;
$date = date("Y-m-d",strtotime("$date +1 day"));
}while($date!=date("Y-m-d",strtotime("$last +1 day")));
echo $days;
?>
- 4 回答
- 0 關注
- 113 瀏覽
添加回答
舉報