我试图计算两个日期之间的工作日和周末的计数,但是对于周末我得到0值。
以下是代码:
<?php
function daysCount($startDate, $endDate)
{
$weekdayCount = $weekendCount =0;
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24))
{
if (date("N", $i) <= 5)
{
$weekdayCount = $weekdayCount + 1;
}
if (date("N", $i) == 6 && $i%7 == 0 )
{
$weekendCount = $weekendCount + 1;
}
}
return array('weekdayCount' => $weekdayCount, 'weekendCount' => $weekendCount);
}
$startDate = "2017-07-03";
$endDate = "2017-07-10";
$days = daysCount($startDate, $endDate);
print_r($days);
?>
这是演示链接demo updated
有人可以帮助我获得周末计划吗?
答案 0 :(得分:1)
也许这样,基于演示链接:
function number_of_working_days($startDate, $endDate)
{
$workingDays = 0;
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
if (date("N", $i) <= 5) $workingDays = $workingDays + 1;
}
return $workingDays;
}
function number_of_weekend_days($startDate, $endDate)
{
$weekendDays = 0;
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
for ($i = $startTimestamp; $i <= $endTimestamp; $i = $i + (60 * 60 * 24)) {
if (date("N", $i) > 5) $weekendDays = $weekendDays + 1;
}
return $weekendDays;
}
$startDate = "2016-11-01";
$endDate = "2016-11-30";
$workingDays = number_of_working_days($startDate, $endDate);
$weekendDays = number_of_weekend_days($startDate, $endDate);
echo "Total number of working days between $startDate and $endDate is: " . $workingDays . " day(s).";
echo "Total number of weekend days between $startDate and $endDate is: " . $weekendDays . " day(s).";