0

I want to get the time difference between 2 dates in minutes. The 2 dates are in the following format

date1='05-11-2012 11:25:00'
date2='06-11-2012 17:45:00'
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
user1490612
  • 289
  • 2
  • 4
  • 8
  • Refer this http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php – Unni Nov 05 '12 at 06:25

4 Answers4

2
$datetime1 = new DateTime('05-11-2012 11:25:00');
$datetime2 = new DateTime('06-11-2012 17:45:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); //return +1 days
//or
$datetime1 = date_create('05-11-2012 11:25:00');
$datetime2 = date_create('06-11-2012 17:45:00');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); //return +1 days

for php <5.3

$date1 = '05-11-2012 11:25:00';
$date2 = '06-11-2012 17:45:00';
$diff = floor(abs( strtotime( $date1 ) - strtotime( $date2 ) )/(60*60*24));
printf("%d days\n", $diff); //return 1 days
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
1
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

For further reference please visit the following link http://php.net/manual/en/datetime.diff.php

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
1

You neglected to indicate what format you'd like the time difference expressed in. If you're willing to work in seconds (which are easily converted), you could simply convert each date to a timestamp and then take the absolute difference using basic math. For instance, give $date1 = '05-11-2012 11:25:00' and $date2 = '06-11-2012 17:45:00':

$diff_seconds = abs( strtotime( $date1 ) - strtotime( $date2 ) );

And you could do whatever you like with the result.

Kevin Nielsen
  • 4,413
  • 21
  • 26
0

$start_time = strtotime( "2012-10-12 11:35:00" );

$end_time = strtotime( "2012-10-13 12:42:50" );

echo round ( abs( $end_time - $start_time ) / 60,2 ). " minute";

This is different between two dates in MINUTES.

softsdev
  • 1,478
  • 2
  • 12
  • 27