首页 > 解决方案 > 如何找到2次之间的差异?

问题描述

插入时间的控制器功能:

$input = $this->input->post('shiftStartTime');
$input = str_replace(" : ", ":", $input);
$shiftstarttime = date('H:i:s', strtotime($input));

$input2 = $this->input->post('shiftEndTime');
$input2 = str_replace(" : ", ":", $input2);
$shiftEndTime = date('H:i:s', strtotime($input2));

我想取2次之间的时间差

这是我的代码,但显示错误的时间差。如何解决?

$input3 = $this->input->post('shiftEndTime')-$this->input->post('shiftStartTime');
$input3 = str_replace(" : ", ":", $input3);
$duration = date('H:i:s', strtotime($input3));

标签: phpcodeignitertimecontroller

解决方案


试试这个diff功能

<?php
$input = $this->input->post('shiftStartTime');
$input = str_replace(" : ", ":", $input);
$shiftstarttime = date('H:i:s', strtotime($input));

$input2 = $this->input->post('shiftEndTime');
$input2 = str_replace(" : ", ":", $input2);
$shiftEndTime = date('H:i:s', strtotime($input2));
echo date_create($shiftEndTime)->diff(date_create($shiftstarttime))->format('%H:%i:%s');
?>

推荐阅读