PHP: Calculate date differences in days, hours, and more
- Date Differences Calculations in PHP
- Using DateTime and diff
- Difference in Days
- Difference in Hours
- Difference in Months
- Difference in Seconds
- Difference in Years
- Difference From Now
- Difference in Minutes
- Difference in Hours and Minutes
- Summary of Recommended Approaches
Date Differences Calculations in PHP
Date calculations are essential for tasks such as scheduling, expiration checks, analytics, and time based validation. PHP provides accurate and reliable tools for these operations through DateTime, DateInterval, and timestamps. In this blog post, I'll demonstrate how to calculate date differences in every common unit with concise examples.
Using DateTime and diff
DateTime represents a point in time. The diff method compares two DateTime objects and returns a DateInterval.
$date1 = new DateTime('2024-01-01');$date2 = new DateTime('2024-02-10');$interval = $date1->diff($date2);
A DateInterval exposes properties such as y, m, d, h, i, s. It handles leap years, month boundaries, and daylight changes automatically.
Difference in Days
Using diff:
$date1 = new DateTime('2024-01-01');$date2 = new DateTime('2024-01-20');$days = $date1->diff($date2)->days;echo $days;
The days property returns the total difference in days.
Using timestamps:
$seconds = abs(strtotime('2024-01-01') - strtotime('2024-01-20'));$days = intdiv($seconds, 86400);echo $days;
Use timestamps when you only need complete day counts without calendar rules.
Difference in Hours
Using DateInterval:
$date1 = new DateTime('2024-01-01 00:00');$date2 = new DateTime('2024-01-02 05:00');$diff = $date1->diff($date2);$hours = $diff->days * 24 + $diff->h;echo $hours;
Using timestamps:
$seconds = abs(strtotime('2024-01-01 00:00') - strtotime('2024-01-02 05:00'));$hours = intdiv($seconds, 3600);echo $hours;
Difference in Months
The DateInterval exposes years and months. Total months is years multiplied by 12 plus months.
$date1 = new DateTime('2023-01-01');$date2 = new DateTime('2024-04-01');$diff = $date1->diff($date2);$months = $diff->y * 12 + $diff->m;echo $months;
DateInterval correctly accounts for varying month lengths.
Difference in Seconds
Using timestamps is the simplest method.
$seconds = abs(strtotime('2024-01-01 12:00') - strtotime('2024-01-01 12:10'));echo $seconds;
Use timestamps when you need exact second-level differences.
Difference in Years
DateInterval automatically handles leap years.
$date1 = new DateTime('2020-02-29');$date2 = new DateTime('2024-02-28');$years = $date1->diff($date2)->y;echo $years;
Difference From Now
Comparing any date to the current moment:
$target = new DateTime('2024-12-25');$now = new DateTime();$daysFromNow = $now->diff($target)->days;echo $daysFromNow;
This produces the total number of days from today to the target date.
Difference in Minutes
Using timestamps:
$seconds = abs(strtotime('2024-01-01 12:00') - strtotime('2024-01-01 12:45'));$minutes = intdiv($seconds, 60);echo $minutes;
Converting from DateInterval:
$diff = (new DateTime('12:00'))->diff(new DateTime('12:45'));$minutes = $diff->h * 60 + $diff->i;echo $minutes;
Difference in Hours and Minutes
DateInterval can format the result directly.
$date1 = new DateTime('2024-01-01 08:00');$date2 = new DateTime('2024-01-01 12:45');echo $date1->diff($date2)->format('%h hours %i minutes');
Or compute total minutes and split:
$seconds = abs(strtotime('08:00') - strtotime('12:45'));$totalMinutes = intdiv($seconds, 60);$hours = intdiv($totalMinutes, 60);$minutes = $totalMinutes % 60;echo $hours . ' hours ' . $minutes . ' minutes';
Summary of Recommended Approaches
DateTime with diff is ideal for calendar aware calculations such as months, years, and full date comparisons. Timestamps are ideal for raw elapsed seconds, minutes, and hours when calendar rules are irrelevant. Use the DateInterval format method for clean combined output such as hours and minutes.
Stay Updated.
I'll you email you as soon as new, fresh content is published.