-1

Can date_diff be used to compute difference in minutes?

$a   = date_create('2016-11-03 00:00:00');
$b   = date_create('2016-11-05 00:19:00');

$dd = date_diff($a, $b);
echo $dd->format('%i');

The above code will return 19 minutes even if it's already been 2 days

IMB
  • 15,163
  • 19
  • 82
  • 140
  • 2
    Possible duplicate of [get php DateInterval in total 'minutes'](http://stackoverflow.com/questions/16776061/get-php-dateinterval-in-total-minutes) – Peter Nov 05 '16 at 11:30
  • date_diff will return the DateInterval object. You can't get complete time in minutes with that. If really want complete minutes build a own logic to calculate that. – vijaykumar Nov 05 '16 at 11:39
  • Possible duplicate of [How to get time difference in minutes in PHP](http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – 19greg96 Nov 05 '16 at 16:53

2 Answers2

4

Try this

it will output difference in minutes.

$a = new DateTime('2016-11-05 00:00:00');
$b = new DateTime('2016-11-05 00:19:00');
 $diff =  ($b->getTimestamp() - $a->getTimestamp())/60;
 echo $diff;
shubham715
  • 3,324
  • 1
  • 17
  • 27
0

You can use DateTime()
Get difference in hour,minute, and seconds

$a =   new \DateTime("2016-11-05 00:00:00");
$b =   new \DateTime("2016-11-05 00:19:00");

$dateinterval = $b->diff($a);

$time = sprintf(
    '%d:%02d:%02d',
    ($dateinterval->d * 24) + $dateinterval->h,
    $dateinterval->i,
    $dateinterval->s
);
Rajesh Patel
  • 1,946
  • 16
  • 20