-1

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?

Pankaj
  • 9,749
  • 32
  • 139
  • 283

2 Answers2

1

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)));
Eric
  • 311
  • 1
  • 6
1

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.

0stone0
  • 34,288
  • 4
  • 39
  • 64