2014-09-30 250 views
0

如果有人能帮助我,这将是伟大的确定它是简单的东西,但对于我的生活我无法找到如何做到这一点我有两个时间戳,我想将它们加在一起( $ timestamp1 + $ timestamp2)我有第一部分想通了,但我不能得到我想要这样的结果到目前为止,我有这个两个时间戳总结在一起

$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分○○秒这是完美 第二代码我有这

$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']; 

} 

并与此代码我得到th是28:07:00这是完美的

我需要的是这16时20分00秒+ 28:07:00

然而,当我试图给他们以各种方式,我知道可能加在一起,它不会工作所以我难倒请谁能帮助我

谢谢

+1

想要的结果是什么? – 2014-09-30 14:22:14

+0

您可能正在使用**绝对时间**,这意味着时间戳具有**年,月和日**信息。这就是为什么它可能不起作用。 – tfrascaroli 2014-09-30 14:25:37

+0

[这个问题](http://stackoverflow.com/questions/11556731/how-we-can-add-two-date-intervals-in-php)(和接受的答案)可能会有所帮助。 – 2014-09-30 14:28:28

回答

2

你在它不打算使用的方式滥用日期/的strtotime系统。例如

strtotime('16:20:00') -> 1412115600 
date('r', 1412115600) -> Tue, 30 Sep 2014 16:20:20 

请注意PHP假设你的“4:20 pm”实际上是“今天”的一部分。这不是“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 
+0

嗨,我刚刚试过你的方式和它工程,但我已经尝试了很多不同的,他们的工作,以及我遇到的问题是完全一样的,你说我需要我的时间去更高,然后24小时,你可以请帮我我已转换为秒,然后将它们添加在一起,然后将其转换回工作,但是我得到的结果是沿着00:00:01的行,而不是更高的24 – user3744228 2014-10-21 09:52:48

0

好总结的时间戳在SQL查询只因为它会更加优化,转换日期时间到UNIX再总结所有的UNIX时间戳并在PHP转换为日期时间。请参阅下面的代码。

$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); 
+0

@ user3744228这是否有帮助? – 2014-09-30 17:57:20