Related Content
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.
Should A Constructor Throw An Exception?
Let's say you had a class that you wanted to use, but there was some sort of error in creating the object. This might be that the wrong parameters were passed, or the third party service (eg. a database) wasn't available at the time of creation.
PHP Question: Variable Reference
Question
What does the following code print out?
function arrayPrint($array)
{
echo implode(' ', $array);
}
$arrayA = [1, 2, 3];
$arrayB = $arrayA;
$arrayB[1] = 0;
arrayPrint($arrayA);
Creating An Authentication System With PHP and MariaDB
Using frameworks to handle the authentication of your PHP application is perfectly fine to do, and normally encouraged. They abstract away all of the complexity of managing users and sessions that need to work in order to allow your application to function.
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.
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