简体   繁体   中英

How to find difference between two times?

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>';

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

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM