如何使用PHP获得总工​​作时间

时间:2017-10-24 10:07:06

标签: php

我必须找到工作人员的总工作时间。

我手中的变量是,

$work_time_start = "8:00:00";
$work_time_end = "18:00:00";
$lunch_break_start = "13:00:00";
$lunch_break_end = "14:00:00";

由此我必须找到工作人员的总工作时间。

例如。

//example 1    

$work_start_time = "14:00:00";
$work_end_time = "14:25:00";

//The output should be: 00:25:00

//example 2

$work_start_time = "14:00:00";
$work_end_time = "10:25:00";

//The output should be: 06:25:00

如何实现所需的输出。我不知道。

3 个答案:

答案 0 :(得分:1)

按要求提供详细答案。

$work_time_start = "8:00:00";
$work_time_end = "18:00:00";
$lunch_break_start = "13:00:00";
$lunch_break_end = "14:00:00";

$worktime = convert_to_seconds($work_time_end) - convert_to_seconds($work_time_start);

$lunchtime = convert_to_seconds($lunch_break_end) - convert_to_seconds($lunch_break_start);

$worktime = $worktime - $lunchtime;
echo $worktime;


function convert_to_seconds($str_time)
{

    $str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);

    sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

    $time_seconds = $hours * 3600 + $minutes * 60 + $seconds;

    return $time_seconds;
}

答案 1 :(得分:-1)

希望这有帮助

$work_start_time =new DateTime("14:00:00");
$work_end_time = new DateTime("14:25:00");
$interval = $work_end_time->diff($work_start_time);

echo $interval->format('%H:%I:%S');

答案 2 :(得分:-1)

使用以下代码:

$work_time_start = "8:00:00";
$work_time_end = "18:00:00";
$lunch_break_start = "13:00:00";
$lunch_break_end = "14:00:00";
$total_time = strtotime($lunch_break_start) - strtotime($work_time_start) + strtotime($work_time_end) - strtotime($lunch_break_end);
echo date("h:i:s", mktime(0,0, round($total_time) % (24*3600)));