1

i have a huge problem with datetime difference. When i compare this two dates 2019-12-01 and 2019-11-01, there is only 30 days difference and no month, why?

$date2 = new DateTime('2019-12-01');
$date1 = new DateTime("2019-11-01");
$diff = $date1->diff($date2, true)->m;
echo "Difference should be 1 month: ".$diff;
Rexima
  • 27
  • 1
  • 6

1 Answers1

0

Do this way

$date2 = new DateTime("2019-12-01T00:00:00Z");
$date1 = new DateTime("2019-11-01T00:00:00Z");
$diff = $date1->diff($date2, true)->m;
echo "Difference should be 1 month: ".$diff;

The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time.

The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).

Explained here

See demo here

unclexo
  • 3,691
  • 2
  • 18
  • 26