PHP Calculate Difference Between Two Dates In Years, Months, And Days

PHP Calculate Difference Between Two Dates In Years, Months, And Days

When you are working with dates in PHP. And you need to calculate the difference between two dates in years, months and days. Or you have to find the total number of months and a total number of days between two dates. You can do all the calculations between two dates using PHP date functions.

So, 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

By using the following methods, you can calculate the difference between two dates in years, months and days with PHP. And also can calculate number of months and days between two dates in PHP:

  • 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.

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *