尝试计算跟踪某些任务的时间。我需要考虑一些我首先用一些IF语句检查的休息时间,然后我需要在花费时间到数据库之前从总时间中减去所有休息时间。开头和结尾的大部分来自一个HTML表单,输入类型为" time"以及代码中的硬编码,如下所示。他们被认为是字符串吗?
我想让这件事发生。 ("完成时间" - "开始时间" - "所有休息" ="总时间")。但不知怎的,我不能让这项工作......
这是我的代码:
//Check for breaks
if ($time_start <= "10:00:00" and $time_finish >= "10:10:00"){
$halftime_morning = "00:10:00";
} else $halftime_morning = "";
if ($time_start <= "11:20:00" and $time_finish >= "11:50:00"){
$lunch = "00:30:00";
} else $lunch = "";
if ($time_start <= "15:00:00" and $time_finish >= "15:10:00"){
$halftime_afternoon = "00:10:00";
} else $halftime_afternoon = "";
if ($time_start <= "17:00:00" and $time_finish >= "17:30:00"){
$dinner = "00:30:00";
} else $dinner = "";
//Calculates the total working hours
$total_working_time = new DateTime($time_finish);
$total_working_time->diff(new DateTime($time_start));
echo "Working time :" . $total_working_time->format("H:i:s") . "\n";
$halftime = new DateTime($halftime_morning);
$halftime->add(new DateInterval($halftime_afternoon));
//echo "Working time" . $halftime->format('H:i:s') . "\n";
$food = new DateTime($lunch);
$food->add(new DateInterval($dinner));
//echo "Food" . $food->format("H:i:s") . "\n";
$break = new DateTime($food);
$break->add(new DateInterval($halftime));
//echo "Break" .$break->format("H:i:s") . "\n";
$total_working_time = new DateTime($working_time);
$total_working_time->diff(new DateTime($break));
//$total_working_time = "";
还尝试在这样的中断上添加新的DateTime():
$dinner = new DateTime("00:30:00");
我尝试过的另一种解决方案:
$break = (strtotime($halftime_morning) + strtotime($lunch) + strtotime($halftime_afternoon) + strtotime($dinner));
echo date('H:i:s', $break );
Stack Overflow的示例也尝试了
$origin = '00:00:00';
$newTotal = '00:45:00';
$oldTotal = '00:16:00';
$added = strtotime($newTotal) + (strtotime($oldTotal) - strtotime($origin));
echo date('H:i:s', $added )
这是我在第一段代码中出现的错误之一:
Working time :21:30:00 Catchable fatal error:
Object of class DateTime could not be converted to string in filename.php on line 72
第72行是我在SQL中插入数据的代码。我的代码中有很多或错误试图操纵之前的时间。
我真的不明白如何解决这个问题。据我所知,部分问题在于我试图将字符串与时间匹配。是我的表格中的数据(输入类型(时间)是字符串还是时间?如果我使用&#34;&#34;或&#34; 00:00:00&#34;对我有用吗?休息?
有一段时间我得到了#34;差异&#34;使用DateTime但从不&#34;添加&#34;。目前它甚至没有从完成时间中减去开始时间..
有什么建议吗?
编辑:我想我需要澄清一下。我的时间永远不算正确。对于我的总时间,开始时间永远不会被减去,而剩下的休息时间我会得到错误,指的是混合字符串和时间。
新例子: 以下代码以某种方式输出&#34; 2:20&#34;当我使用秒而不是1:20这是正确的..
//Check for breaks
if ($time_start <= "10:00:00" and $time_finish >= "10:10:00"){
$halftime_morning = (10*60);//"00:10:00";
} else $halftime_morning = 0;
if ($time_start <= "11:20:00" and $time_finish >= "11:50:00"){
$lunch = (30*60);//""00:30:00";
} else $lunch = 0;
if ($time_start <= "15:00:00" and $time_finish >= "15:10:00"){
$halftime_afternoon = (10*60);//"00:10:00";
} else $halftime_afternoon = 0;
if ($time_start <= "17:00:00" and $time_finish >= "17:30:00"){
$dinner = (30*60);//"00:30:00";
} else $dinner = 0;
$break = 0;
$break = ($halftime_morning + $lunch + $halftime_afternoon + $dinner);
echo date('H:i:s', $break );
答案 0 :(得分:0)
您可以尝试将所有字符串转换为时间戳。与他们合作更容易。
我尝试了这个,它应该是你预期的输出。
$time_start = strtotime("10:00:00")%(60*60*24); //10:00 in seconds
$time_end = strtotime("17:30:00")%(60*60*24); //17:30 in seconds
$halftime_morning = 10*60; // 10 minutes - 600 seconds
$lunch = 30*60; //30 minutes - 1800 seconds
$halftime_afternoon = 10*60; //10 minutes - 600 seconds
$dinner = 30*60; //30 minutes - 1800 seconds
$totalBreak = $halftime_morning + $lunch + $halftime_afternoon + $dinner;
echo gmdate('H:i:s', $totalBreak );
当您检查时,时间是在特定的晚餐/午餐时间之间等,您还必须将此值转换为时间戳