Reputation: 527
I was thinking this should be simple.
$m_time1 = strtotime('1:00:00');
$m_time2 = strtotime('5:30:00');
$m_total = $m_time1 + $m_time2;
echo date('h:i:s', $m_total);
The result is 3:30:00
but should be 6:30:00
Any clue why?
Upvotes: 0
Views: 1857
Reputation: 8349
strtotime()
produces a unix timestamp which represents the number of seconds between the time provided and January 1st 1970. Since you didn't specify a date in your function call, it assumes the current date at the time you passed to the function.
As a result your code above, run today produces an output of
$m_time1 = 1376024400
$m_time2 = 1376040600
When you add these together it results in a "time" of 3:30 AM
in the year 2057
.
To avoid this happening, you need to escape subtract the timestamp for "today" from the timestamps before adding them and then add it back again after the addition.
$today = strtotime("TODAY");
$m_time1 = strtotime('1:00:00') - $today;
$m_time2 = strtotime('5:30:00') - $today;
$m_total = $m_time1 + $m_time2 + $today;
echo date('h:i:s', $m_total);
The above code echos 6:30:00
.
Upvotes: 2