mysql - 如何计算每行的 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.")");
解决方案
也许您的“时间戳”实际上是 unix 时间戳——自 1970 年 1 月 1 日以来的秒数。如果是这样,只需算术即可满足您的要求:
SELECT SUM(endtime - starttime) / 60 as total_minutes
FROM elbat;
推荐阅读
- pyspark - 无法运行程序“python”:错误=2,通过 gitlab ci/cd 管道运行 pyspark 测试时没有这样的文件或目录
- deployment - 如何在没有 Nginx 的情况下以通用模式部署 Nuxt?
- spring-security-rest - 如何在使用 Spring Security 的 Spring Boot 应用程序中跳过对 HAL 浏览器的授权
- php - 在特定日期更改 sql 表中的数据值?
- c - 在 C 中将单词转换为长字
- c++ - C 宏的标准合规性
- windows - SAPI 语音可以与 Microsoft Speech API 一起使用吗?
- spring - 是否可以将 thymeleaf 配置为从数据库呈现 html?
- avro - AVRO 需要为每个字段设置一个值
- java - 如何从 ChipGroup 获得精选的芯片?