我有这个代码,对于某些日期工作正常,我不明白为什么,下面的代码应该计算总天数。如果我选择leavefrom = 2014-04-21和leaveto = 2014-05-02,它总共给我8天,但应该是9天9天。
这是日历: -
function total_day($leavefrom, $leaveto){
$start_date=strtotime($leavefrom);
$cur_day=$start_date;
$end_day=strtotime($leaveto);
$count=0;
$holiday=array("2014-05-01"=>"Labour Day", "2014-08-31"=>"Independence Day", "2014-12-25"=>"Christmas");
while(1){
//echo date("Y/m/d", $cur_day)."<br/>";
$cur_day=$cur_day +(3600*24);
//echo $count."S--".date("Y-m-d", $cur_day)."<-----E--".$end_day;
$day_of_week=date('w', $cur_day);
//echo "day_of_week-----".$day_of_week."<br/>";
if ($day_of_week == 0 || $day_of_week == 6) {
//No Operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
//echo "Holiday because of ".$holiday[date("Y-m-d", $cur_day)];
}else{
$count++;
}
//echo "Total day--".$count."<br/><br/>";
if(($cur_day==($end_day+(3600*24)))||($cur_day>$end_day)){
break;
}
}
//$count = $count + 1;
return $count;
}
$totaldays = total_day($leavefrom, $leaveto);
答案 0 :(得分:0)
我看到没有错误。从2014-04-21
到2014-05-02
有11天,其中2个周末和1个假期(2014-05-01
)。从而制作11 days - 3 days = 8 days
但是如果你想包括开始日期。然后你可以使用它:
while(1){
if($cur_day>$end_day){
break;
}
$day_of_week=date('l', $cur_day);
if (in_array($day_of_week,array('Saturday','Sunday'))) {
// NO operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
// NO operation
}else{
$count++;
}
$cur_day=$cur_day +(3600*24);
}