php - 如何找到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));
解决方案
试试这个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');
?>
推荐阅读
- python - 根据Django中的今天日期查找上一个星期天的日期
- spring-security - 即使我们输入不安全的内容,Spring Security 也会在会话过期后重定向到登录页面
- haskell - function composition, interact and hackerrank exercises
- python - 创建一个数字的倍数列表
- gitlab - 如果标准方式显示错误 500,我如何删除损坏的 GitLab 存储库?
- popsql - 有这样的页面是什么意思
- c# - C# WPF TreeViewItem MouseDoubleClick 事件在 TreeViewItem 与 TreeViewItem.Header
- machine-learning - 训练期间的模型评估是否会影响 PyTorch 中的最终准确性?
- java - 返回不工作(需要实例化一个对象?) - Java
- python - Flask-SQLAlchemy 不会因连接丢失而超时