Reputation: 277
I have two dates in 24 hour format
$fulldate_hour_start = "2013-11-11 18:16:00 ";
$fulldate_hour_end = "2013-11-11 23:30:00 ";
and I subtracted them using this
$Hours = number_format((((strtotime($fulldate_hour_end) - strtotime($fulldate_hour_start)) / 60) / 60),2);
and the result is 5 hours and 23 minutes. it should be 5 hours and 14 minutes. What is my error?
Upvotes: 2
Views: 1754
Reputation: 4331
Try this :
$date1 = new DateTime("2013-11-11 18:16:00");
$date2 = new DateTime("2013-11-11 23:30:00");
$interval = $date1->diff($date2);
echo "difference " . $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds ";
Upvotes: 4
Reputation: 181077
Since an hour consist of 60, not 100, minutes, 5.23 hours is 5 hours and 13.8 minutes. The 0.2 hour mismatch is due to rounding the real value to two decimals to get 0.23.
0.23 * 60 = 13.8
Upvotes: 0
Reputation: 10000
You're dividing by 3600 to convert from seconds to hours and then using number_format
to render the result with two decimal places.
The result of the expression was:
5.23
Which is indeed approximately 5 hours and 14 minutes. (Remember that an hour has 60 minutes.)
Rather than do this, you should keep the value in seconds, and then use a function to convert it to a human readable format. One possibility is date_diff
; the linked SO question has many others.
Upvotes: 0