用PHP和DateTime计算时间,不能用“diff”和“add”得到正确的时间


Count time with PHP and DateTime, can not get the right time with "diff" and "add"

试图计算跟踪某些任务的时间。我需要考虑一些休息时间,我首先用一些 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 = "";

还尝试在这样的休息时间添加新的日期时间():

$dinner = new DateTime("00:30:00");

我尝试过的另一个解决方案:

$break = (strtotime($halftime_morning) + strtotime($lunch) + strtotime($halftime_afternoon) + strtotime($dinner));
echo date('H:i:s', $break );

堆栈溢出的示例也尝试过

$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 中插入数据的代码。我的代码中有很多错误试图操纵之前的时间。

我真的不明白如何解决这个问题。据我了解,我的部分问题是我正在尝试将字符串与时间匹配。表单(输入类型(时间))中的数据是字符串还是时间?我使用"或"00:00:00"进行休息有关系吗?

有一段时间,我得到了"diff"来使用日期时间,但从未"添加"。目前,它甚至没有从完成时间中减去开始时间。

有什么建议吗?

编辑:我想我需要澄清一下。我的时间永远不会被正确计算。对于我的总时间,开始时间永远不会被减去,其余的休息时间我会收到错误,将我推荐为混合字符串和时间。

新示例:当我使用秒而不是 1:20 时,以下代码以某种方式输出"2: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 );

您可以尝试将所有字符串转换为时间戳。与他们合作更容易。

我试过这个,它应该是你预期的输出。

$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 );

当您检查时间是否介于晚餐/午餐等的特定时间之间时,您还必须将此值转换为时间戳