Php 两个时间戳相加
如果有人能帮我这将是伟大的,我相信这是一些简单的事情,但对于我的生活,我不知道如何做到这一点,我有两个时间戳,我想把它们加在一起($timestamp1+$timestamp2)我已经计算出了第一部分,但我无法得到我想要的结果,到目前为止我有这个Php 两个时间戳相加,php,mysql,Php,Mysql,如果有人能帮我这将是伟大的,我相信这是一些简单的事情,但对于我的生活,我不知道如何做到这一点,我有两个时间戳,我想把它们加在一起($timestamp1+$timestamp2)我已经计算出了第一部分,但我无法得到我想要的结果,到目前为止我有这个 $flight_time = strtotime($flight_time_1)+strtotime($flight_time_2)+strtotime($flight_time_3)+strtotime($flight_time_4)+strtoti
$flight_time = strtotime($flight_time_1)+strtotime($flight_time_2)+strtotime($flight_time_3)+strtotime($flight_time_4)+strtotime($flight_time_5)+strtotime($flight_time_6)+strtotime($flight_time_7)+strtotime($flight_time_8)+strtotime($flight_time_9)+strtotime($flight_time_10);
$flight_time = date("H:i:s", $flight_time);
这给了我16:20:00的时间,这是完美的
我的第二个代码是
$sqlcow = "SELECT system_flight FROM uav_checks WHERE uav_id = '$registration'";
$result1cow=mysql_query($sqlcow);
while ($arow = mysql_fetch_array($result1cow)){
$system_flight2 = $arow['system_flight'];
}
用这个代码我得到28:07:00这是完美的
我需要的是16:20:00+28:07:00
然而,当我试图以各种方式将它们组合在一起时,我知道这是不可能的,所以我很困惑,请有人能帮助我吗
谢谢你你在某种程度上滥用了日期/标准时间系统,这是无意使用的。e、 g
strtotime('16:20:00') -> 1412115600
date('r', 1412115600) -> Tue, 30 Sep 2014 16:20:20
请注意PHP如何假设您的“下午4:20”实际上是“今天”的一部分。这不是“16小时*3600秒/小时+20分钟*60秒/分钟”->58800秒
strtotime('16:20:00') + strtotime('01:02:30')
1412115600 + 1412060523
2824176123
date('r', 2824176123) -> Sun, 29 Jun 2059 23:22:03
考虑一下如果你的时间串加起来超过24小时会发生什么,例如
16:20:00 + 7:40:01 -> 23:60:01 -> 1day 00:00:01
现在,您的H:i:s
值将显示00:00:01的时间,这是一次非常短的飞行
您需要手动将时间值转换为秒,例如
(16 * 3600) + (20 * 60) + (0) -> 57600 + 1200 -> 58800
(01 * 3600) + (02 * 60) + (30) -> 3600 + 120 + 30 -> 3750
58800 + 3750 -> 62550
然后返回到h:m:s格式:
17:22:30
我们只在sql查询中求和时间戳,因为它将得到更优化,将datetime转换为unix,然后求和所有unix时间戳,并在php中转换为datetime。请参考下面的代码
$sqlcow = "SELECT SUM(UNIX_TIMESTAMP(system_flight))
FROM uav_checks WHERE uav_id = '$registration'";
$result1cow=mysql_query($sqlcow);
while ($arow = mysql_fetch_array($result1cow)){
$system_flight2 = $arow['system_flight'];
}
echo gmdate("H:i:s\Z", $system_flight2);
期望的结果是什么?您可能使用的是绝对时间,这意味着时间戳也包含年、月和日信息。这就是它可能不起作用的原因。期望的结果是一个变量,它包含两个变量的总和$system\U flight2+$flight\U time(16:20:00+28:07:00)=44:27:00(以及公认的答案)可能会有帮助。@user3744228这有帮助吗?嗨,我刚刚尝试了你的方法,它可以工作,但是我尝试了很多不同的方法,它们都很有效我遇到的问题正是你所说的我需要时间去提高到24小时以上你能帮我吗我已经转换成秒,然后把它们加在一起,然后再转换回来它有效,然而,我得到的结果是00:00:01,而不是24