How can i calculate hours between two dates in php?
How do I calculate the difference between two dates in hours? Show
For example:
In this case the result should be 47 hours.
asked Jun 24, 2010 at 9:17
3 The newer PHP-Versions provide some new classes called
The DateInterval-object, which is returned also provides other methods than
And here are the links for documentation:
All these classes also offer a procedural/functional way to operate with dates. Therefore take a look at the overview: http://php.net/manual/book.datetime.php
Glavić 41.6k13 gold badges74 silver badges107 bronze badges answered Jun 24, 2010 at 9:47
FidiFidi 5,5541 gold badge17 silver badges25 bronze badges 7
answered Jun 24, 2010 at 9:20
Jan HančičJan Hančič 52.3k16 gold badges94 silver badges99 bronze badges 3 To provide another method for Count Hours https://3v4l.org/Mu3HD
Result
Count Hours with Daylight Savings https://3v4l.org/QBQUBPlease be advised that See the current bug report
Result
answered Mar 4, 2016 at 16:57
Will B.Will B. 16.3k4 gold badges61 silver badges67 bronze badges 6 your answer is:
answered Jun 24, 2010 at 9:20
Sergey EreminSergey Eremin 10.8k2 gold badges37 silver badges44 bronze badges 1 The easiest way to get the correct number of hours between two dates (datetimes), even across daylight saving time changes, is to use the difference in Unix timestamps. Unix timestamps are seconds elapsed since 1970-01-01T00:00:00 UTC, ignoring leap seconds (this is OK because you probably don't need this precision, and because it's quite difficult to take leap seconds into account). The most flexible way to convert a datetime string with optional timezone information into a Unix timestamp is to construct a DateTime object (optionally with a DateTimeZone as a second argument in the constructor), and then call its getTimestamp method.
answered Jan 29, 2014 at 21:30
Walter TrossWalter Tross 11.9k2 gold badges38 silver badges61 bronze badges 3
answered Apr 11, 2014 at 5:51
answered Jan 26, 2014 at 0:31
1
I guess strtotime() function accept this date format. answered Jun 24, 2010 at 9:21
Boris DelormasBoris Delormas 2,4891 gold badge19 silver badges27 bronze badges Unfortunately the solution provided by FaileN doesn't work as stated by Walter Tross.. days may not be 24 hours! I like to use the PHP Objects where possible and for a bit more flexibility I have come up with the following function:
This like
N.B. I have used answered May 6, 2015 at 14:07
ArthArth 12.3k5 gold badges35 silver badges68 bronze badges 0 Carbon could also be a nice way to go. From their website:
Example:
Carbon extends the DateTime class to inherit methods including answered Apr 19, 2018 at 6:17
joshuamabinajoshuamabina 1,34017 silver badges25 bronze badges This function helps you to calculate exact years and months between two given dates,
geomagas 3,2101 gold badge16 silver badges27 bronze badges answered Jun 24, 2010 at 15:08 1 To pass a unix timestamp use this notation
answered Dec 11, 2016 at 16:06
1 First, you should create an interval object from a range of dates. By the wording used in this sentence alone, one can easily identify basic abstractions needed. There is an interval as a concept, and a couple of more ways to implement it, include the one already mentioned -- from a range of dates. Thus, an interval looks like that:
When you have an interval, you can format it however you like. If you need a number of full hours, you can have
If you want a ceiled total hours, here you go:
For more about this approach and some examples, check out this entry. answered May 9, 2020 at 13:59
Vadim SamokhinVadim Samokhin 3,3044 gold badges39 silver badges66 bronze badges In addition to @fyrye's very helpful answer this is an okayish workaround for the mentioned bug (this one), that DatePeriod substracts one hour when entering summertime, but doesn't add one hour when leaving summertime (and thus Europe/Berlin's March has its correct 743 hours but October has 744 instead of 745 hours): Counting the hours of a month (or any timespan), considering DST-transitions in both directions
P.S. If you check a (longer) timespan, which leads to more than those two transitions, my workaround won't touch the counted hours to reduce the potential of funny side effects. In such cases, a more complicated solution must be implemented. One could iterate over all found transitions and compare the current with the last and check if it is one with DST true->false. answered Jul 29, 2020 at 10:30
spackmatspackmat 8529 silver badges23 bronze badges
You can try this one.
fcdt 2,2615 gold badges12 silver badges26 bronze badges answered Aug 15, 2020 at 18:30 0 This is working in my project. I think, This will be helpful for you. If Date is in past then invert will 1.
answered Nov 16, 2016 at 5:49
Gurudutt SharmaGurudutt Sharma 4842 gold badges6 silver badges18 bronze badges How can I calculate the number of hours between two dates in PHP?Show activity on this post. $day1 = "2006-04-12 12:30:00" $day1 = strtotime($day1); $day2 = "2006-04-14 11:30:00" $day2 = strtotime($day2); $diffHours = round(($day2 - $day1) / 3600);
How do I calculate total hours in PHP?There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format.
How can I get days between two dates in PHP?The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.
How can I get hours and minutes in PHP?Try this: $hourMin = date('H:i'); This will be 24-hour time with an hour that is always two digits. For all options, see the PHP docs for date().
|