Unfortunately the solution provided by FaileN doesn't work as stated by Walter Tross. $d2 = new DateTime($str2, $tz2) // and ignored if str contains tz offset $d1 = new DateTime($str1, $tz1) // tz is optional, 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. Unix timestamps are seconds elapsed since T00: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 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. See the current bug report //set timezone to UTC to disregard daylight savingsĭate_default_timezone_set('America/New_York') So its usage is subjective to your desired outcome and date range. Please be advised that DatePeriod excludes an hour for DST but does not add another hour when DST ends. count the number of objects within the periods $periods = new \DatePeriod($start, $interval, $end) create periods every hour between the two dates determine what interval should be used - can change to weeks, months, etc Count Hours $start = new \DateTime('T12:30:00') To provide another method for DatePeriod when using the UTC or GMT timezone. If you want the result in hours only, you could to something like this: $date1 = new DateTime('T12:30:00') Īnd here are the links for documentation:Īll these classes also offer a procedural/functional way to operate with dates. The DateInterval-object, which is returned also provides other methods than format. Call the format method on the DateInterval-objectĮcho $diff->format('%a Day and %h hours') The return value more specifically represents the clock-time interval to apply to the original object ( this or originObject) to arrive at the targetObject. The diff-methods returns a new DateInterval-object. absolute Should the interval be forced to be positive Return Values The DateInterval object represents the difference between the two dates. Here's what you want with the help of this objects: // Create two new DateTime-objects. And on top of that it's very easy to use. The cool thing about this classes is, that it considers different timezones, leap years, leap seconds, summertime, etc. format('%s Seconds %i Minutes %h Hours %d days %m Months %y Year Ago').The newer PHP-Versions provide some new classes called DateTime, DateInterval, DateTimeZone and DatePeriod. I have used a function that would get me the difference between the date passed and the server's date. "49 seconds ago", "20 minutes ago", "21 hours ago" and so on And then, we use strtotime() to convert the date string into timestamp to calculate the difference. In this example, we are start and end dates from the user. In this tutorial, we are using PHP date time functions to calculate the hour difference between two dates. I wrote this function for one my blog site(difference between a past date and server's date). In PHP, there are many ways to calculate the difference between two dates. The DateTime class will take daylight saving (depending on timezone) into account where the "old way" won't. Which is the actual number of minutes that has passed between the two dates. To get the total number of minutes: $minutes = $since_start->days * 24 * 60 Note that the days property is available (because we used the diff method of the DateTime class to generate the DateInterval object). $since_start = $start_date->diff(new DateTime(' 10:25:00')) The DateTime::diff () function is an inbuilt function in PHP which is used to return the difference between two given DateTime objects. Use the DateTime class to do any date calculations now that PHP 5.3 is the norm.Įg. The answers above are for older versions of PHP.
0 Comments
Leave a Reply. |