In this tutorial, you will learn how to calculate the difference between two dates in years, months, and days with PHP.
How to Calculate the Difference Between Two Dates In Years, Months, And Days with PHP?
Here are some methods:
- Method 1: Using the DateTime class with diff() Function
- Method 2: Using the strtotime() function
- Method 3: Using the date_diff() function
Method 1: Using the DateTime class with diff() Function
To calculate the difference between two dates using the diff() method with DateTime class.
Here is an example of how to use the DateTime class to calculate the difference between two dates in years, months, and days:
$date1 = new DateTime('2000-01-01'); $date2 = new DateTime('2023-04-26'); $interval = $date1->diff($date2); echo "Difference between dates: " . $interval->y . " years, " . $interval->m . " months, " . $interval->d . " days";
Method 2: Using the strtotime() function
Using the strtotime(), floor(), and calculation method to find or calculate the difference between two dates in PHP. It returns the years, months, days between two specified dates.
Here is an example of how to use the strtotime() function to calculate the difference between two dates in years, months, and days:
$date1 = strtotime('2000-01-01'); $date2 = strtotime('2023-04-26'); $diff = $date2 - $date1; $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24) / (60*60*24)); echo "Difference between dates: " . $years . " years, " . $months . " months, " . $days . " days";
Here is another example of calculating the number of months between two dates in PHP using strtotime() and normal formula:
$date1 = '2020-01-25'; $date2 = '2023-02-20'; $ts1 = strtotime($date1); $ts2 = strtotime($date2); $year1 = date('Y', $ts1); $year2 = date('Y', $ts2); $month1 = date('m', $ts1); $month2 = date('m', $ts2); $diff = (($year2 - $year1) * 12) + ($month2 - $month1); echo $diff;
Here is another example of finding the number of days between two dates in PHP using strtotime() and normal formula:
<?php $now = time(); // or your date as well $your_date = strtotime("2022-01-31"); $datediff = $now - $your_date; echo round($datediff / (60 * 60 * 24)); ?>
Method 3: Using the date_diff() function
The date_diff() function is a newer addition to PHP that provides a simplified way to calculate the difference between two dates.
Here is an example of how to use the date_diff() function to calculate the difference between two dates in years, months, and days:
$date1 = date_create('2000-01-01'); $date2 = date_create('2023-04-26'); $diff = date_diff($date1, $date2); echo "Difference between dates: " . $diff->y . " years, " . $diff->m . " months, " . $diff->d . " days";
Conclusion
That’s it, in this tutorial, you have learned how to calculate or find the difference between the two dates in years, months, and days using DateTime class, strtotime() function, and date_diff(). And each has its own advantages and disadvantages.
When choosing a method for calculating date differences, consider factors such as ease of use, and performance.