i want to minus two dates in php
for example:
$date1 = 08/16/2013;
$date2 = 08/23/2013;
$answer = date2 - date1;
the $answer should be 7, How will i do that? thank you so much
Start using DateTime class for date/time manipulation :
$date1 = new DateTime('08/16/2013');
$date2 = new DateTime('08/23/2013');
$diff = $date1->diff($date2);
print_r($diff); // or $diff->days
Output :
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 7
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 7
)
Read more for DateTime:diff().
Please note that various strtotime() examples are not correct in date/time difference calculation. The simplest example is difference between 2013-03-31 21:00
and 2013-03-30 21:00
. Which for naked eye is exact 1 day difference, but if you do subtract this 2 dates, you will get 82800
seconds which is 0.95833333333333
days. This is because of the time change from winter to summer time. DateTime handles leap years and time-zones properly.
Try this -
<?php
$date1 = strtotime('08/16/2013');
$date2 = strtotime('08/23/2013');
echo $hourDiff=round(abs($date2 - $date1) / (60*60*24),0);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With