Difference between two dates in years, months, days in PHP

Today, We want to share with you Difference between two dates in years, months, days in PHP.In this post we will show you calculate the difference between two dates in PHP, hear for PHP Get difference between two dates in years months days hours minutes we will give you demo and example for implement.In this post, we will learn about php difference between two dates in years months and days with an example.

Difference between two dates in years, months, days in PHP

There are the Following The simple About Difference between two dates in years, months, days in PHP Full Information With Example and source code.

As I will cover this Post with live Working example to develop calculate days between two dates in php, so the PHP Date Exercises Time difference php calculate time difference between two dates in minutes for this example is following below.

PHP Difference Between Two Dates

PHP Date Exercises Time difference in days and years, months, days, hours, minutes, seconds between two dates

$first_date = new DateTime('2019-06-01 02:12:51');
$second_date = new DateTime('2023-05-12 11:10:00');
$live_date_diff = $first_date->diff($second_date);
echo $live_date_diff->days.'Total days'."\n";
echo $live_date_diff->y.' years'."\n";
echo $live_date_diff->m.' months'."\n";
echo $live_date_diff->d.' days'."\n";
echo $live_date_diff->h.' hours'."\n";
echo $live_date_diff->i.' minutes'."\n";
echo $live_date_diff->s.' seconds'."\n";

You can also make a single simple PHP function/Methods to fetch difference between two dates using PHP in years months, days, hours and minutes that will be very helpful for all some filter data or chart or any project. We won’t need to re-write the source code.

//get Simple difference between two dates in years months, days, hours and minute
$live_date_diff = getDateDiff('2019-06-01 02:12:51','2022-05-12 11:10:00');

print_r($live_date_diff);

function getDateDiff($first_date,$second_date) {
    $first_date = new DateTime($first_date);
    $second_date = new DateTime($second_date);
    $live_date_diff = $first_date->diff($second_date);
    
    $nodeResult['total_days'] = $live_date_diff->days;
    $nodeResult['years'] = $live_date_diff->y;
    $nodeResult['months'] = $live_date_diff->m;
    $nodeResult['days'] = $live_date_diff->d;
    $nodeResult['hours'] = $live_date_diff->h;
    $nodeResult['minutes'] = $live_date_diff->i;
    $nodeResult['seconds'] = $live_date_diff->s ;
    return $nodeResult;
}
Angular 6 CRUD Operations Application Tutorials

difference between two dates in php

In PHP, you can calculate the difference between two dates using the DateTime class and the diff() method. Here’s an example code snippet:

$date1 = new DateTime('2022-01-01');
$date2 = new DateTime('2023-02-17');
$diff = $date2->diff($date1);
echo $diff->format('%a days'); // output: 412 days

In this example, we create two DateTime objects representing the dates “2022-01-01” and “2023-02-17”, respectively. Then we use the diff() method to calculate the difference between these two dates. The result is a DateInterval object that contains information about the time difference.

Finally, we use the format() method to output the difference in days. The ‘%a’ placeholder represents the number of days between the two dates, and the ‘days’ string is the unit we want to display. You can also format the difference in other units such as weeks, months, or years by using different placeholders in the format string.

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Difference between two dates in years, months, days in PHP.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment