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 关注
- 94 浏览
添加回答
举报