-1

I am using the following code for finding the time difference but it gives me wrong time difference. I followed following links this and this but the difference is wrong.

$diff = date_diff(date_create(date("Y-m-d h:i A", $this->start_time)), date_create(date("Y-m-d h:i A")));
echo '<span style = "color: #739e3b">'.$diff->d.' Day(s) : '.$diff->h.' Hr(s) : '.$diff->i.' Min(s)</span>';
Bizley
  • 17,392
  • 5
  • 49
  • 59
Bilal Saqib
  • 25
  • 1
  • 6

2 Answers2

5

Procedural:

<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

This is the OOP way:

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

Here's the documentation: http://php.net/manual/en/datetime.diff.php And here's a great library to make your life easier: https://carbon.nesbot.com

Marina Mosti
  • 461
  • 2
  • 7
  • Timezones may be tricky: https://stackoverflow.com/questions/51982412/php-diff-gives-incorrect-values-where-date1-2016-03-01 – rob006 Sep 25 '18 at 16:41
0
$start = date_create('2015-01-26 12:01:00');
$end = date_create('2015-01-26 13:15:00');
$diff=date_diff($end,$start);
print_r($diff);
rawathemant
  • 744
  • 1
  • 4
  • 16