Example 1 — Difference Between Two Dates
Compare two fixed dates and read the months, days and total days from the returned DateInterval.
<?php
$a = date_create('2024-01-15');
$b = date_create('2024-03-20');
$diff = date_diff($a, $b);
echo "Months: ", $diff->m, PHP_EOL;
echo "Days: ", $diff->d, PHP_EOL;
echo "Total days: ", $diff->days, PHP_EOL;
echo $diff->format('%m months, %d days'), PHP_EOL; How It Works
- date_diff() compares January 15 with March 20 and returns a DateInterval.
- The m and d properties give the calendar breakdown of 2 months and 5 days.
- The days property counts every day between the dates, which is 65 because 2024 is a leap year.
- format() writes the interval using placeholders such as %m and %d.

