Php convert seconds to days, hours, minutes
Asked 10 years, 10 months ago Show Viewed 183k times I would like to convert a variable Example:
Result should be:
Glavić 41.7k13 gold badges74 silver badges107 bronze badges asked Nov 25, 2011 at 20:14
0 This can be achieved with Function:
Use:
demo
Pikamander2 6,3433 gold badges42 silver badges63 bronze badges answered Oct 30, 2013 at 11:18
GlavićGlavić 41.7k13 gold badges74 silver badges107 bronze badges 11 This is the function rewritten to include days. I also changed the variable names to make the code easier to understand...
Source: CodeAid() - http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)
Matthew 27.3k26 gold badges100 silver badges163 bronze badges answered Nov 25, 2011 at 20:17
Julian MorenoJulian Moreno 1,0443 gold badges15 silver badges30 bronze badges 6 Based on the answer by Julian Moreno, but changed to give the response as a string (not an array), only include the time intervals required and not assume the plural. The difference between this and the highest voted answer is: For
For
I hope this helps someone. answered May 13, 2017 at 19:04
Luke CousinsLuke Cousins 2,0191 gold badge19 silver badges37 bronze badges 2 Here it is a simple 8-lines PHP function that converts a number of seconds into a human readable string including number of months for large amounts of seconds: PHP function seconds2human()
answered Aug 25, 2012 at 1:25
snippetsofcodesnippetsofcode 9372 gold badges10 silver badges10 bronze badges 3
Result will be 19 23:41:07. Even if the time is an extra 1 second, it causes the day to change. So it turns out 19. You can explode the result for your needs and fix this. answered Oct 13, 2014 at 16:41
4 There are some very good answers here but none of them covered my needs. I built on Glavic's answer to add some extra features that I needed;
You can see a running version of the code here.
answered May 10, 2018 at 20:26
Ramy NasrRamy Nasr 2,28719 silver badges24 bronze badges 2 Short, simple, reliable :
answered Jun 19, 2017 at 22:30
3 Laravel example 700+ locales support by Carbon
answered Dec 23, 2020 at 11:41
the_hasanovthe_hasanov 7297 silver badges14 bronze badges The simplest approach would be to create a method that returns a DateInterval from the DateTime::diff of the relative time in $seconds from the current time $now which you can then chain and format. For example:-
Now chain your method call to DateInterval::format
Result:
answered Dec 11, 2016 at 21:11
Although it is quite old question - one may find these useful (not written to be fast):
result:
answered Nov 29, 2013 at 13:53
ArturArtur 6,9402 gold badges24 silver badges38 bronze badges
answered Apr 12, 2016 at 12:10
James DohertyJames Doherty 1,1711 gold badge7 silver badges6 bronze badges 1 Solution that should exclude 0 values and set correct singular/plural values
answered Sep 14, 2017 at 10:09
radzsergradzserg 1,0981 gold badge9 silver badges22 bronze badges
answered Jan 8, 2018 at 13:07
0 I don't know why some of these answers are ridiculously long or complex. Here's one using the DateTime Class. Kind of similar to radzserg's answer. This will only display the units necessary, and negative times will have the 'ago' suffix...
EDIT: If you want to condense the above example down to use less variables / space (at the expense of legibility), here is an alternate version that does the same thing:
answered Jan 24, 2020 at 17:42
JasonJason 831 silver badge6 bronze badges 2 an extended version of Glavić's excellent solution , having integer validation, solving the 1 s problem, and additional support for years and months, at the expense of being less computer parsing friendly in favor of being more human friendly:
answered Nov 24, 2016 at 2:43
hanshenrikhanshenrik 18.1k3 gold badges39 silver badges77 bronze badges
answered Oct 18, 2021 at 10:42
1 All in one solution. Gives no units with zeroes. Will only produce number of units you specify (3 by default). Quite long, perhaps not very elegant. Defines are optional, but might come in handy in a big project.
Feel free to down-vote, but be sure to try it in your code. It might just be what you need. answered Dec 10, 2016 at 6:51
Interval class I have written can be used. It can be used in opposite way too.
More info here https://github.com/LubosRemplik/CakePHP-Interval answered Jan 11, 2017 at 9:44
With DateInterval :
answered Mar 16, 2018 at 13:02
ZayonZayon 112 bronze badges A bit more elaborated, skipping the time units which are zero
answered Dec 21, 2021 at 13:04
Daniel J.Daniel J. 2901 silver badge10 bronze badges I think Carbon will give you all variety that you want so for your example you will add this code
the output will be like this 2 weeks 4 days 23 hours 41 minutes answered Dec 29, 2021 at 14:56
Ahmed BermawyAhmed Bermawy 2,0904 gold badges35 silver badges41 bronze badges Here's some code that I like to use for the purpose of getting the duration between two dates. It accepts two dates and gives you a nice sentence structured reply. This is a slightly modified version of the code found here.
Source: https://gist.github.com/ozh/8169202 answered May 26, 2016 at 10:01
The solution for this one I used (back to the days while learning PHP) without any in-functions:
Though this was an old question, new learners who come across this, may find this answer useful. answered Jan 5, 2019 at 8:50
answered Nov 24, 2019 at 14:35
I am editing one of the code to work it well when negative value comes.
answered Jun 1, 2020 at 12:04
A variation on @Glavić's answer - this one hides leading zeros for shorter results and uses plurals in correct places. It also removes unnecessary precision (e.g. if the time difference is over 2 hours, you probably don't care how many minutes or seconds).
answered Jun 10, 2020 at 19:30
Mike FurlenderMike Furlender 3,7895 gold badges46 silver badges72 bronze badges Added some formatting modified from Glavić's great answer for Facebook style time of post count up....
answered Jul 6, 2021 at 8:48
$value come from Database. This code is in Codeigniter. $SESSION is used for storing user subscriptions. it is mandatory. I used it in my case, you can use whatever you want. answered Jun 17, 2019 at 9:02
Tayyab HayatTayyab Hayat 7371 gold badge8 silver badges21 bronze badges 2 This is a function i used in the past for substracting a date from another one related with your question, my principe was to get how many days, hours minutes and seconds has left until a product has expired :
answered Jan 6, 2015 at 12:59
Krazii KiiDKrazii KiiD 1231 gold badge2 silver badges9 bronze badges 1 How to convert seconds into days hours and minutes in php?php function convert_seconds($seconds) { $dt1 = new DateTime("@0"); $dt2 = new DateTime("@$seconds"); return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds'); } echo convert_seconds(200000). "\n"; ?>
How do you convert seconds to hours and minutes?In this case, the minute digits 30 are associated with the quantity one half. Converting between hours, minutes, and seconds using decimal time is relatively straightforward: time in seconds = time in minutes * 60 = time in hours * 3600. time in minutes = time in seconds / 60 = time in hours * 60.
How do I convert seconds to hours in mysql?mysql> SELECT t1, -> TIME_TO_SEC(t1) AS 'seconds', -> TIME_TO_SEC(t1)/60 AS 'minutes', -> TIME_TO_SEC(t1)/(60*60) AS 'hours', -> TIME_TO_SEC(t1)/(24*60*60) AS 'days' -> FROM time_val; +----------+---------+---------+-------+------+ | t1 | seconds | minutes | hours | days | +----------+---------+---------+-------+------ ...
How can I get minutes and seconds in PHP?php //PHP program to convert seconds into //hours, minutes, and seconds $seconds = 6530; $secs = $seconds % 60; $hrs = $seconds / 60; $mins = $hrs % 60; $hrs = $hrs / 60; print ("HH:MM:SS-> " . (int)$hrs .
|