다음과 같은 날짜 범위가 있다고 가정 해 보겠습니다.
2014-01-10 11:00-2014-01-13 15:00
또한 09:00-16:00 사이의 시간 만 계산할 수 있다는 것도 알고 있습니다. PHP로 어떻게이 작업을 수행 할 수 있습니까?
위의 날짜 범위는 25 시간 (5 + 7 + 7 + 6)을 제공해야하지만 그러한 PHP 함수가 어떻게 보이는지 확실하지 않습니다.
나는 지금 해결책을 만들었지 만 하루라도 내 계산에 만족하지 않습니다. 다음은 코드입니다.
$startDate = strtotime("2014-01-22 15:00");
$endDate = strtotime("2014-01-24 18:00");
$time1 = new DateTime(date("H:i", $startDate));
$time2 = new DateTime(date("H:i", $endDate));
$date1 = new DateTime(date("Y-m-d", $startDate));
$date2 = new DateTime(date("Y-m-d", $endDate));
$start = new DateTime("11:00");
$end = new DateTime("18:00");
$maxPerDay = $end->diff($start)->format("%h") != 0 ? $end->diff($start)->format("%h") : 24;
$days = $date2->diff($date1)->format("%a");
$hours = 0;
//Is it just one day and is time intersecting?
if($days == 0 && ($start <= $time2 && $time1 <= $end)){
$hours = $maxPerDay;
if($start < $time1 || $end > $time2){
if($time1 < $start){
$hours = $maxPerDay - $time2->diff($end)->format("%h");
}else if($time2 > $end){
$hours = $maxPerDay - $time1->diff($start)->format("%h");
}else{
$hours = $time1->diff($time2)->format("%h");
}
}
}else if($days > 0){
$firstDay = 0;
$lastDay = 0;
if($time1 < $end){
$firstDay = $time1 < $start ? $maxPerDay : $maxPerDay - $time1->diff($start)->format("%h");
}
if($time2 > $start){
$lastDay = $time2 > $end ? $maxPerDay : $maxPerDay - $time2->diff($end)->format("%h");
}
$hours = $lastDay + $firstDay + ($days - 1) * $maxPerDay;
}
echo $hours;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다