Calculate the difference between two dates using PHP

Calculate the difference between two dates using PHP

In this Post We Will Explain About is Calculate the difference between two dates using PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to calculate hours between two times in php Example

In this post we will show you Best way to implement difference between two dates and time in php, hear for php difference between two dates in years months and days with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

difference between two dates and time in php

Generally It is often handy task to have a proper date function method to get number of total days and get number of some months between two particuler dates. In this POST, We are Learning how to calculate the two dates difference between two dates using PHP.

Calculate months between two dates

For the PHP >=5.3 version We can use simple function DateTime diff that returns a pariticuler DateInterval object as some below code.

$first_dt = new DateTime("2017-12-09");
$second_dt = new DateTime("2018-03-17");
 
var_dump($first_dt->diff($second_dt)->m); // int(3)
var_dump($first_dt->diff($second_dt)->m + ($first_dt->diff($second_dt)->y*12)); // int(3)

If We don’t have version of PHP 5.3 or higher, We can use simple strtotime() function to get pariticuler timestamps,and then the number of seconds between dates format any date and Like as a January 1 1970 00:00:00.

$first_dt = "2017-12-09";
$second_dt = "2018-03-17";
echo (int)abs((strtotime($first_dt) - strtotime($second_dt))/(60*60*24*30)); // 3

The PHP ABS() function absolute value to often return a simple positive number as the all simple number of months between the two any particuler dates.But this is not very precise so there is not always get the 30 days per month.
this Can be an option to pariticuler get months between two any dates.

$first_dt = strtotime("2017-12-09");
$second_dt = strtotime("2018-03-17");
echo abs((date('Y', $second_dt) - date('Y', $first_dt))*12 + (date('m', $second_dt) - date('m', $first_dt))); // 3

Other Way to calculate the some month difference

$first_dt = strtotime("2017-12-09");
$second_dt = strtotime("2018-03-17");
$date_minimum = min($first_dt, $second_dt);
$date_maximum = max($first_dt, $second_dt);
$i = 0;
 
while (($date_minimum = strtotime("+1 MONTH", $date_minimum)) <= $date_maximum) {
    $i++;
}
echo $i; // 3

If We want to include any remaining some days or get current month we can add +1 to the same result.

Calculate days between two dates:

Using simple way to DateTime for PHP >=5.3 version

$first_dt = new DateTime("2017-12-09");
$second_dt = new DateTime("2018-03-17");
echo $first_dt->diff($second_dt)->days; // 98

IMP Note: and there for windows 7 there is a simple bug, It may always date return 6015 as a result for like as a $first_dt->diff($second_dt)->days returns instead of 98.

For PHP same as version of the PHP < 5.3 we can use simple PHP function timestamps to get some days between any two dates using this strtotime() function.

$first_dt = "2017-12-09";
$second_dt = "2018-03-17";
 
$days = floor(abs(strtotime($second_dt) - strtotime($first_dt))/(60*60*24));
echo $days. "Days";

We may use below some source code also to calculate two days between dates.

$first_dt = strtotime("2017-12-09");
$second_dt = strtotime("2018-03-17");
$date_minimum = min($first_dt, $second_dt);
$date_maximum = max($first_dt, $second_dt);
$i = 0;
 
while (($date_minimum = strtotime("+1 day", $date_minimum)) <= $date_maximum) {
    $i++;
}
echo $i; // 98

Example

I hope you have Got What is How to Get Number of Days Between Two Dates in PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment