Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time diff in minutes between 2 dates

Tags:

php

datetime

Got this working on php 5.3

$datetime1 = new DateTime("2011-10-10 10:00:00");
$datetime2 = new DateTime("2011-10-10 10:45:00");
$interval = $datetime1->diff($datetime2);
$hours   = $interval->format('%h'); 
$minutes = $interval->format('%i');
echo 'Diff. in minutes is: '.($hours * 60 + $minutes); 

How can i make it work on php 5.2 ? are there any equivalent functions available??

Got it working

$date1 = "2011-10-10 10:00:00";
$date2 = "2011-10-10 10:11:00";
echo round((strtotime($date2) - strtotime($date1)) /60);
like image 767
sam Avatar asked Oct 28 '11 13:10

sam


People also ask

How do I calculate the number of minutes between two dates?

To get the number of minutes between 2 dates: Get the number of milliseconds between the unix epoch and the Dates. Subtract the milliseconds of the start date from the milliseconds of the end date. Divide the result by the number of milliseconds in a minute - 60 * 1000 .

How do I calculate minutes between two dates in Excel?

=TEXT(TODAY()-D5,"[mm]") Here, the TODAY()-D5 part of the formula returns the difference between the today and the date in D5 in days. The TEXT Function converts this difference to minutes. Press ENTER and you will see the time difference between today and the date in cell D5 by minute.


1 Answers

Instead of DateTime you can use strtotime and date:

$datetime1 = strtotime("2011-10-10 10:00:00");
$datetime2 = strtotime("2011-10-10 10:45:00");
$interval  = abs($datetime2 - $datetime1);
$minutes   = round($interval / 60);
echo 'Diff. in minutes is: '.$minutes; 
like image 139
hsz Avatar answered Oct 04 '22 21:10

hsz