Php 计算工作时间
我试图得到两个日期之间的时差,考虑假期和开始和结束工作时间 这是我的密码:Php 计算工作时间,php,Php,我试图得到两个日期之间的时差,考虑假期和开始和结束工作时间 这是我的密码: function business_hours($start, $end, $holidays = null){ $startDate = new DateTime($start); $endDate = new DateTime($end); $periodInterval = new DateInterval('PT1H'); $period = new DatePeriod($st
function business_hours($start, $end, $holidays = null){
$startDate = new DateTime($start);
$endDate = new DateTime($end);
$periodInterval = new DateInterval('PT1H');
$period = new DatePeriod($startDate, $periodInterval, $endDate);
$count = 0;
foreach($period as $date){
#clone $date to get properties
#set start time of working hours to 8:00
$startofday = clone $date;
$startofday->setTime(8,30);
#clone $date to get properties
#set end time of working hours to 17:00
$endofday = clone $date;
$endofday->setTime(17,30);
#this will be used for conditional check
$notHoliday = true;
#now check the array of holiday and check if in range dates is same with holidays we have
if($holidays != null && is_array($holidays) && in_array($date->format('Y-m-d'), $holidays)){
$notHoliday = false;
}
#check if $date is greater than $startofdate and less that the $endofday and does not fall on Saturday or Sunday
#also check if the hours of date is less than or equal to 12 or greater that 13 and $notHoliday equals true
if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Saturday', 'Sunday')) && ($date->format('H') <= 12 || $date->format('H') > 13) && $notHoliday){
$count++;
}
}
return $count;
}
function business\u hours($start、$end、$holidays=null){
$startDate=新日期时间($start);
$endDate=新日期时间($end);
$periodInterval=新的日期间隔('PT1H');
$period=new DatePeriod($startDate,$periodInterval,$endDate);
$count=0;
foreach($期间为$日期){
#克隆$date以获取属性
#将工作时间的开始时间设置为8:00
$startofday=克隆$date;
$startofday->setTime(8,30);
#克隆$date以获取属性
#将工作时间的结束时间设置为17:00
$endofday=克隆$date;
$endofday->setTime(17,30);
#这将用于条件检查
$notHoliday=真;
#现在检查假日数组,并检查范围内的日期是否与我们的假日相同
if($holidays!=null&&is_数组($holidays)&&in_数组($date->format('Y-m-d'),$holidays)){
$notHoliday=假;
}
#检查$date是否大于$startofdate且小于$endofday且不在周六或周日
#还要检查日期的小时数是否小于或等于12或大于13且$NOTOLIDAY等于true
如果($date>$startofday&&$date格式('l')、数组('Saturday'、'Sunday'))和($date->format('H')格式('H')>13)和($noholiday){
$count++;
}
}
返回$count;
}
我的代码的问题是它不计算微小的差异。我想包括分钟计算,以获得准确的工作时间。您可以尝试php的packageCarbon,它可以用diffInMinutes()方法减去2个日期并返回准确的分钟数。这里有这么多示例,它们没有帮助吗?例如,,,等等,@Don'tPanic我搜索了一些例子,但没有一个能应用于我想要实现的目标