Find Number of Months & Days Between Two Dates in PHP

diff($date2);

echo $interval->format(‘%m months and %d days’);

// Output: 2 months and 30 days

Sometimes you need to find out the number of months and number of days between two or more dates. There are many functions or methods in PHP for this. Using these, you can find/get/calculate them between 2 or more dates.

In this tutorial, you will learn how to find or calculate number of days and month between two dates in PHP.

How to Find the Number of Months Between Two Dates in PHP

By using the following ways, you can easily get/find/calculate numbers of months and days between two dates in PHP:

  • Calculate the number of months between two dates in PHP
  • Calculate the number of days between two dates in PHP

Calculate the number of months between two dates in PHP

Using the PHP DateTime(), diff(), and strtotime() functions, you can get or calculate the number of months between two dates in PHP.

How to use DateTime() and diff() method to find the number of months between two dates? Here is first example of this:

$date1 = new DateTime('2022-03-15');
$date2 = new DateTime('2023-01-01');
$interval = $date1->diff($date2);
$months = $interval->y * 12 + $interval->m;
echo "Number of months between the two dates: " . $months;

How to use strtotime() and round() method to find the number of months between two dates? Here is another example of this:

$date1 = strtotime('2022-03-15');
$date2 = strtotime('2023-01-01');
$months = round(abs($date2 - $date1) / (30.44 * 24 * 60 * 60));
echo "Number of months between the two dates: " . $months;

Calculate the number of days between two dates in PHP

Also, you can use the PHP DateTime(), diff(), and strtotime() functions, To get or calculate the number of days between two dates in PHP.

How to use DateTime() and diff() method to find the number of days between two dates? Here is first example of this:

$date1 = new DateTime('2022-03-15');
$date2 = new DateTime('2023-01-01');
$interval = $date1->diff($date2);
$days = $interval->days;
echo "Number of days between the two dates: " . $days;

How to use strtotime() and round() method to find the number of days between two dates? Here is another example of this:

$date1 = strtotime('2022-03-15');
$date2 = strtotime('2023-01-01');
$days = round(abs($date2 - $date1) / (60 * 60 * 24));
echo "Number of days between the two dates: " . $days;

Conclusion

In conclusion, there are different ways to find the number of months and days between two dates in PHP. But in this tutorial, you have learned simple and easy ways to find number of months and days between two dates.

Recommended Tutorials


Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?