2017-07-10 43 views
-1

我试图得到两个日期之间的工作日和周末计数,但对于周末我得到0值。得到工作日和周末的计数从给出两个日期在PHP

下面是代码:

<?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

有人可以帮助我获得weekendCount?

+0

那不是相同的代码您的演示,该代码我们纠正? – Flosculus

+0

也是周末,这是否意味着作为一个组?例如星期六和星期日将算作1个周末?或者说,周六和周日都是单独的周末? – ThisGuyHasTwoThumbs

+0

@Flosculus:我给出的代码正在纠正 – 06011991

回答

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)."; 
+0

谢谢你工作:) – 06011991

相关问题