我想计算一些时间。我需要知道从呼叫输入到现在的工作时间之间有多少工作时间。
例如:电话存储昨天 15:00工作时间结束: 18:00工作时间开始: 08:00现在: 10:00
所以我需要知道我的电话在工作时间的年龄:
- 通话存储>>工作时间结束:3小时
- 工作时间现在开始>>: 2小时
- 年龄:5小时
我想使用php DateTime。
你会怎么做?
我认为这可以帮助您:
//workdays till 18:00 and start 08:00
function calculateWorkHours(DateTime $Start, DateTime $End) {
//validate given parameters
if ($Start > $End) throw new Exception('$Start date cannot be later than $End date');
$hours = 0;
do {
//get the current hour
$currentHour = $Start->format('H');
//while the $currenthour is lower then 18 and higher than 7
if($currentHour < 18 && $currentHour >= 8) {
$hours++;
}
$Start->modify('+1 hour');
} while($End > $Start);
return $hours;
}
$Start = new DateTime('yesterday 150000');
$End = new DateTime('100000');
echo calculateWorkHours($Start, $End); //returns 5
$Start = new DateTime('yesterday 060000');
$End = new DateTime('120000');
echo calculateWorkHours($Start, $End); //returns 14
$Start = new DateTime('-2 days 150000');
$End = new DateTime('100000');
echo calculateWorkHours($Start, $End); //return 15