Reputation: 15
if anyone caould help me with this that would be great im sure it is something simple but for the life of me i cant find out how to do this i have two timestamps and i want to add them together ($timestamp1 + $timestamp2) i have the first part figured out but i cant get the result i want so far i have this
$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);
and that gives me the time 16:20:00 which is perfect the second code i have is this
$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'];
}
and with this code i get this 28:07:00 which is perfect
what i need is this 16:20:00 + 28:07:00
however when i try to add them together in every way i know possible it wont work so i am stumped please can anyone help me
thank you
Upvotes: 0
Views: 195
Reputation: 4166
Well sum up timestamp in sql query only as it will be more optimized, convert datetime to unix and then sum all unix timestamps and in php convert to datetime. Please refer to code below.
$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);
Upvotes: 0
Reputation: 360862
You're abusing the date/strtotime system in a way it's not intended to be used. e.g.
strtotime('16:20:00') -> 1412115600
date('r', 1412115600) -> Tue, 30 Sep 2014 16:20:20
note how PHP has assumed your "4:20pm" is actually part of "today". It's not "16 hours * 3600 seconds/hour + 20 minutes * 60 seconds/minute" -> 58800 seconds.
strtotime('16:20:00') + strtotime('01:02:30')
1412115600 + 1412060523
2824176123
date('r', 2824176123) -> Sun, 29 Jun 2059 23:22:03
Consider what happens if your time strings add up to more than 24 hours, e.g.
16:20:00 + 7:40:01 -> 23:60:01 -> 1day 00:00:01
Now your H:i:s
value will show a time of 00:00:01, which is a very very short flight.
You need to convert your time values to seconds manually, e.g.
(16 * 3600) + (20 * 60) + (0) -> 57600 + 1200 -> 58800
(01 * 3600) + (02 * 60) + (30) -> 3600 + 120 + 30 -> 3750
58800 + 3750 -> 62550
and then back to h:m:s format:
17:22:30
Upvotes: 2