Code
$date1 = date('Y/m/d', strtotime("Oct 09 2018"));
$date2 = date('Y/m/d', strtotime("Sep 09 2018"));
$dateDiff = $date2 - $date1;
echo (round(($dateDiff)/ (60 * 60 * 24)));
It displays 0. Am I missing anything?
Code
$date1 = date('Y/m/d', strtotime("Oct 09 2018"));
$date2 = date('Y/m/d', strtotime("Sep 09 2018"));
$dateDiff = $date2 - $date1;
echo (round(($dateDiff)/ (60 * 60 * 24)));
It displays 0. Am I missing anything?
Date() returns a string value. You can just ditch it altogether if you are comparing dates:
$date1 = strtotime("Oct 09 2018");
$date2 = strtotime("Sep 09 2018");
$dateDiff = $date2 - $date1;
echo (round(($dateDiff)/ (60 * 60 * 24)));
You're trying to subtract $date2
from $date1
, however, date('Y/m/d', strtotime("Oct 09 2018"))
returns something like 2018/10/09
, you cant subtract those.
strtotime()
returns an unix timestamp, perfect to subtract those to get the diff:
<?php
$date1 = strtotime("Oct 09 2018");
$date2 = strtotime("Sep 09 2018");
$dateDiff = $date2 - $date1;
echo (round(($dateDiff)/ (60 * 60 * 24)));
Will ouput -30
as you can see in this online demo.