88278

PHP calc difference between time greater than 24 hours

Question:

What is the best way to calculate the difference in time, when time is greater than 24 hours.

<strong>Example</strong>

$time1 = '76:00:00'; $time2 = '30:00:00'; // result should be 46:00:00 echo date('H:i:s', strtotime($time1) - strtotime($time2));

But this could not be done with this because its greater then 24 hours.

Also in a database i've stored a time like this: 33:30:00 How in php could i format it to: 33:30

Answer1:

Use \DateTime and \DateInterval to perform calculations:

$date1 = new \DateTime('now', new DateTimeZone('UTC')); $date2 = new \DateTime('now', new DateTimeZone('UTC')); $time1 = new \DateInterval('PT76H'); $time2 = new \DateInterval('PT30H'); $date1->add($time1); $date2->add($time2); $diff = $date1->diff($date2); echo ($diff->days * 24 + $diff->h) . $diff->format(':%I:%S');

Explanation: It's not possible to perform calculations directly on DateIntervals, so you have to create dates as a basis for calculations. Then add two different intervals to current dates, and calculate a difference between them. diff() returns \DateInterval that contains total number of days, that you have to multiply by 24 to get hours, and hours that don't make full days.

EDIT: The timezone should be specified as UTC to avoid daylight saving time issues.

Recommend

  • How to get date on last/previous saturday in given timezone
  • Convert datetime to a different time zone
  • The datetime zone id 'america' is not recognised in Elasticasearch script
  • php timezone returns empty array
  • Vb.net Add years and then go to last day of the month
  • PHP calc difference between time greater than 24 hours
  • Split an array in sub arrays of consecutive dates
  • PHP getting month dates and placing into array
  • How to calculate a moving average in MySQL in a correlated subquery?
  • Convert int 60 to a time value of 60 minutes
  • Angularjs. ng-model is not being updated in datepicker
  • Firebase Save Notification to DB not working when app is not running
  • date.js Parse method overrides Javascript Parse method
  • How to parse utc date
  • toInstant() in Calendar is showing in GMT instead of Local time
  • Excel Date field value differs from c# dateTime by 1 day while reading excel file with EEPlus
  • R sqldf renaming a field in a select statement
  • Unable to get column index with table.getColumn method using custom table Model
  • Keep this build forever option - Jenkins
  • Transactional Create with Validation in ServiceStack Redis Client
  • Converting a WriteableBitmap image ToArray in UWP
  • Hardware Accelerated Image Scaling in windows using C++
  • ActiveRecord query for a count of new users by day
  • Illegal mix of collations for operation for date/time comparison
  • Is there any way to access browser form field suggestions from JavaScript?
  • Hazelcast - OperationTimeoutException
  • Upload files with Ajax and Jquery
  • Font Awesome Showing Box instead of Icons
  • Do I've to free mysql result after storing it?
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • A cron job substitute?
  • json Serialization in asp
  • Suggestions to manage Login/Logout transitions
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?