首页 > 解决方案 > 如何计算每行的 2 个时间戳之间的总差异

问题描述

我有一个包含三列的表:

ID - 开始时间(时间戳) - 结束时间(时间戳)

如何计算该表的总分钟数,以及每行的每个时间戳差异的总和?

<input name="datum" type="date" class="form-control" value="<?php echo date("Y-m-d"); ?>">

<input name="starttime" type="time" class="form-control" value="<?php echo date("H:i",time()); ?>">

$datum = date('d-m-Y', strtotime($_POST['datum']));
$starttime= date('H:i', strtotime($_POST['starttime']));
$starttimestamp = strtotime($datum.$starttime);
mysqli_query($db, "INSERT INTO plan VALUES (NULL, ".my_id.", ".$starttimestamp.", ".$name.")");

标签: mysqlsql

解决方案


也许您的“时间戳”实际上是 unix 时间戳——自 1970 年 1 月 1 日以来的秒数。如果是这样,只需算术即可满足您的要求:

SELECT SUM(endtime - starttime) / 60 as total_minutes
FROM elbat;

推荐阅读