Related Content
Creating Sparklines In PHP
A sparkline is a very small line graph that is intended to convey some simple information, usually in terms of a numeric value over time. They tend to lack axes or other labels and are added to information readouts in order to expand on numbers in order to give them more context.
PHP:CSI - Improving Bad PHP Logging Code
I read The Daily WTF every now and then and one story about bad logging code in PHP stood out to me. The post looked at some PHP code that was created to log a string to a file, but would have progressively slowed down the application every time a log was generated.
Generating Histogram Colour Analysis Graphs From Images In PHP
If you've ever looked at the settings in a digital camera, or have experience with image processing programs like GIMP, then you may have seen a colour histogram. This is a simple graph that shows the amount of different shades of colour are present in the image.
PHP:CSI - To Switch, Or Not To Switch?
I was writing unit tests for a API mapping function recently and came across this interesting issue. The code I was writing tests for was in a legacy codebase that I was making changes to, and it made sense to have some unit tests in there before I started work to ensure everything worked before and after.
Drupal 9: Generating Header Images For Pages Of Content Using PHP
Embedding image within pages of content helps both within the design of the page and when shared on social media. If you set up meta tags to point at a particular image then that image will appear when the page is shared on social media. This makes your page stand out more.
Using PSR-4 With Composer
The PHP Standards Recommendations (called PSR) are a set of standards that aim to make certain aspects of working with PHP easier.
Comments
Hi 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:
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 :)
Submitted by Anonymous on Mon, 10/03/2011 - 19:32
PermalinkThank 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.
Submitted by philipnorton42 on Mon, 10/03/2011 - 19:55
Permalinkhi tae, thanks..
Submitted by Anonymous on Sun, 12/18/2011 - 19:45
Permalinki propose to change little to allow this kind of input "01:05" =1h05 or "01:05:00" or "01"=1hour
Submitted by ffert2907 on Tue, 09/11/2012 - 12:56
PermalinkThis function kept returning :60 in the seconds place! I added :
to the end of the function.
Submitted by Matthew Galvin on Mon, 04/29/2013 - 21:19
PermalinkMy 2 cents comment if used your function and added a format control to avoid strange behavior and surprise
Thanks a lot for posting
Submitted by Road on Thu, 07/11/2013 - 13:53
PermalinkSorry posted too quickly (fixed preg_match return array)
Submitted by Road on Thu, 07/11/2013 - 14:15
PermalinkSubmitted by Luis on Fri, 02/13/2015 - 13:50
PermalinkError here
and not
Submitted by Gianluca on Mon, 05/04/2020 - 13:25
PermalinkQuite right Gianluca, thanks for pointing that out.
Submitted by philipnorton42 on Mon, 05/04/2020 - 14:16
PermalinkAdd new comment