Related Content
Generating Colour Palettes From Images In PHP
A common web design pattern is to incorporate an image into the design of the page. This creates a tighter integration with the image and the rest of the page.
The main issue in designing a page around the image is that the colours of the page must match the image. Otherwise this creates a dissonance between the image and the styles of the site.
Validating XML Files With XML Schema Definitions In PHP
XML is a useful format for configuration, data storage, and transmitting data from one system to another. As a human readable format that can be easily read by machines it quickly gained favor in lots of different systems as a mechanism for data storage.
Creating A Character Bitmap In PHP
I was watching a video from NDC by Dylan Beattie the other day on The Story of Typography and a particular section stood out to me.
Approximating Pi Using A Circle And A Square
Pi day was a few weeks ago, but I came across this simple approximation of pi recently and decided to put together an example in PHP since it seemed pretty simple.
This approximation of pi centers around a real world example, but we can simulate this using some code.
Drawing A Parabolic Curve With Straight Lines In PHP
A parabolic curve is a type of curve where every point is an equal distance from a focal point. There a number of different way to generate this sort of curve using math, but one of the simplest is to use straight lines to create the illusion of the curve.
Recreating Spotify Wrapped In PHP
I quite like the end of the year report from Spotify that they call "Wrapped". This is a little application in which they tell you what your favorite artist was and what sort of genres you listened to the most during the year.
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 giHlZp8M8D 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 giHlZp8M8D on Mon, 05/04/2020 - 14:16
PermalinkAdd new comment