I have a Two variable with value date type assigned to it. Now I want to find difference of those two variable values.
$d1='2016-08-24 12:22:13';
$d2='2016-08-24 12:22:30';
difference of d2-d1
is 17 seconds. How to find it in php?
I have a Two variable with value date type assigned to it. Now I want to find difference of those two variable values.
$d1='2016-08-24 12:22:13';
$d2='2016-08-24 12:22:30';
difference of d2-d1
is 17 seconds. How to find it in php?
// Instantiate a DateTime
$datetimefirst = new DateTime('2016-08-24 12:20:00');
$datetimesecond = new DateTime('2016-08-24 12:34:00');
//calculate the difference
$difference = $datetimefirst->diff($datetimesecond);
//format the Output
echo $difference->format('%Y-%m-%d %H:%i:%s');
The DateTime class:
This class behaves the same as DateTimeImmutable except objects are modified itself when modification methods such asDateTime::modify()
are called.
Here is the solution,
<?php
$d1='2016-08-24 12:22:13';
$d2='2016-08-24 12:22:30';
$diff=strtotime($d2)-strtotime($d1);
echo $diff;
?>
For get difference between two date you have to convert it into time stamp first and take difference from there.
$d1='2016-08-24 12:22:13';
$d2='2016-08-24 12:22:30';
$diff=abs(strtotime($d2)-strtotime($d1));
echo "Diff ".date('H',$diff)." hours ".date('i',$diff)." minutes ".date('s',$diff)." Seconds";
here I have used abs() for convert into positive value of difference.