Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get interval seconds between two datetime in PHP?

Tags:

php

datetime

2009-10-05 18:11:08

2009-10-05 18:07:13

This should generate 235,how to do it ?

like image 662
Misier Avatar asked Oct 05 '09 10:10

Misier


People also ask

How can I calculate hours between two dates in PHP?

You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How can I get the number of days between two given 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.


1 Answers

With DateTime objects, you can do it like this:

$date = new DateTime( '2009-10-05 18:07:13' ); $date2 = new DateTime( '2009-10-05 18:11:08' );  $diffInSeconds = $date2->getTimestamp() - $date->getTimestamp(); 
like image 164
jonathancardoso Avatar answered Oct 02 '22 05:10

jonathancardoso