Converting To And From Decimal Time In PHP
Published by philipnorton42 on Mon, 11/29/2010 - 14:00To convert a time value into a decimal value representing the number of minutes can be useful for certain calculations. The following function takes a time as a string of hh:mm:ss and returns a decimal value in minutes.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Convert time into decimal time. * * @param string $time The time to convert * * @return integer The time as a decimal value. */ function time_to_decimal($time) { $timeArr = explode(':', $time); $decTime = ($timeArr[0]*60) + ($timeArr[1]) + ($timeArr[2]/60); return $decTime; } |
If we take the time of 11:11:11 this gets split into 3 parts by the explode() function into hours, minutes and seconds, which then gets treated in the following way:
1 2 3 4 | Minutes = (Hours x 60) + (Minutes) + (Seconds / 60) Minutes = (11 x 60) + (11) + (11 / 60) Minutes = (660) + (11) + (0.18333333) Minutes = 671.18333333 |
The function can be used as follows:
1 | echo time_to_decimal("11:11:11"); // prints 671.18333333 |
The reverse of this function takes the decimal value and returns a string in the format hh:mm:ss.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Convert decimal time into time in the format hh:mm:ss * * @param integer The time as a decimal value. * * @return string $time The converted time value. */ function decimal_to_time($decimal) { $hours = floor($decimal / 60); $minutes = floor($decimal % / 60); $seconds = $decimal - (int)$decimal; $seconds = round($seconds * 60); return str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" . str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":" . str_pad($seconds, 2, "0", STR_PAD_LEFT); } |
This function can be used as follows:
1 | echo decimal_to_time(671.18333333); // prints 11:11:11 |
Note that these functions deal with amounts of time, not the time of day. Converting between time and decimal time of day requires a different set of calculations.
UPDATE: Thanks to an anonymous poster for pointing out some errors, these have now been corrected.
Comments
Corrections to errors
Anonymous (not verified) - Mon, 10/03/2011 - 20:32Hi Philip,
Your code indeed works in the manner you have described, however there are a couple of finer details that caused issues for me when using the code.
In the time_to_decimal function, line 12 should read as follows:
$decTime = ($timeArr[0]*60) + ($timeArr[1]) + ($timeArr[2]/60);>As it is written in your post, the seconds are actually representing fractions of an hour (1/3600th) as opposed to fractions of a minute (1/60th).
We have the same issue in the decimal_to_time function as well as an issue in regards to the round() function. I've corrected the decimal_to_time function as follows:
round() in lines 2 and 3 have been replaced by floor(). This is due to the behavior of round() returning 1 when given .5. An example is as follows:
Say we are converting 1:30:30 into a decimal representing minutes. Using the two functions posted we would have
In the example, line 2 of the original code calculates to - round( 90 / 60 ) = round(1.5) = 2. Since we only want the numer of whole hours in the $decimal variable, we should use floor() instead. floor() will always round to the lowest whole number ( 1 in this case )
I hope this helps someone out there :)
Thank you for all the hard
philipnorton42 - Mon, 10/03/2011 - 20:55Thank you for all the hard work you put into that comment! I really appreciate it when a user posts such detailed comments on this blog :)
I have tested your examples and updated my code accordingly.
tae
Anonymous (not verified) - Sun, 12/18/2011 - 20:45hi tae, thanks..
Add new comment