php minutes to hours minutes seconds
Converting seconds to hours/minutes/seconds in PHP [duplicate]
I have an integer as a number of seconds. I want to convert that integer into into hours/minutes/seconds like this:
If the number of seconds equates to less than one hour then it should return the string:
If the number of minutes is less than 10 it should return the string formatted like this:
And finally if the number of seconds equate to less than 1 minute, it should return the following:
What is the best way to do this in PHP?
5 Answers 5
The simpelst approch would be
EDIT: this wouldnt fix your minutes
I think Rufinus is pretty close:
This variant uses a configuration stored inside an array which associates a format string based on a time value in seconds (as key). The last element is the default format that will fall through.
Edit: Unfortunately there is no formatting code in date that allows to specify minutes w/o a leading there. Therefore the date string needs to be re-formatted to remove leading 0’s occasionally. It’s done with ltrim here.
Consider using explode() & implode() and then apply your logic of less-than & greater-than!
Not the answer you’re looking for? Browse other questions tagged php time or ask your own question.
Linked
Related
Hot Network Questions
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How do you convert 00:00:00 to hours, minutes, seconds in PHP?
I have video durations stored in HH:MM:SS format. I’d like to display it as HH hours, MM minutes, SS seconds. It shouldn’t display hours if it’s less than 1.
What would be the best approach?
10 Answers 10
Something like this?
One little change could be:
This will only display the hours or minutes if there are any, and inserts an «and» before the seconds if there are hours, minutes, or both to display. If you wanted to get really fancy, you could add some code to display «hour» vs. «hours» as appropriate, ditto for minutes and seconds.
Why bother with regex or explodes when php handles time just fine?
The benefit is that you can reformat the time however you like (including am/pm, adjustments for timezone, addition / subtraction, etc).
Heres a different way, with different functions which is more open and a more step by step for newbies. it also handles the 1 hour and many hours. you could try use the same logic to handle the 0 minutes and 0 seconds.
I’ll reply with a different approach of the problem. My approach is to store the lengths in seconds. Then depending the needs, it’s easy to render these seconds as hh:mm:ss by using :
or to search on the length in a database:
SELECT * FROM videos WHERE length > 300; for example, to search for video with a length higher than 5 minutes.
If you really want to use a built-in function, perhaps for robustness, you can try
explode() is for pansies. This is a job for regular expressions!
Totally untested, but something like that ought to work. Note that this code assumes that the hour fragment will always be two digits (eg, a three-hour video would be 03:00:00 instead of 3:00:00 ).
EDIT: In retrospect, using regular expressions for this is probably a case of over-engineering; explode() will do the job just as well and probably even be faster in this case. But it was the first method to come to mind when I read the question.
Converting 00:00:00 to hours, minutes, and seconds in PHP is really easy.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Convert seconds to Hour:Minute:Second
I need to convert seconds to «Hour:Minute:Second».
For example: «685» converted to «00:11:25»
How can I achieve this?
28 Answers 28
You can use the gmdate() function:
One hour is 3600sec, one minute is 60sec so why not:
(I’ve not tested this much, so there might be errors with floor or so)
Use function gmdate() only if seconds are less than 86400 (1 day) :
Convert seconds to format by ‘foot’ no limit* :
Example use of DateTime extension:
Input: 6030 Output: 1:40:30
Input: 2000006030 Output: 555557:13:50
Will not give time in H:i:s format if no_of_seconds is greater than 1 day (seconds in a day).
It will neglect day value and give only Hour:Min:Seconds
If you don’t like accepted answer or popular ones, then try this one
Here is a one liner that handles negative seconds and more than 1 day worth of seconds.
write function like this to return an array
then simply call the function like this:
This function my be useful, you could extend it:
Here is a very clean and short method!
This code avoids as much as possible of the tedious function calls and piece-by-piece string-building, and the big and bulky functions people are making for this.
It produces «1h05m00s» format and uses leading zeroes for minutes and seconds, as long as another non-zero time component precedes them.
And it skips all empty leading components to avoid giving you useless info like «0h00m01s» (instead that will show up as «1s»).
Example results: «1s», «1m00s», «19m08s», «1h00m00s», «4h08m39s».
Duration must be 0 or higher in both of the code pieces above. Negative durations are not supported. But you can handle negative durations by using the following alternative code instead: