# PHP Calculate Difference Between Two Dates

## PHP Calculate Difference Between Two Dates

In this Post We Will Explain About is PHP Calculate Difference Between Two Dates 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 The Difference Between Two Dates In PHPExample

In this post we will show you Best way to implement Calculate Duration Between Two Dates using PHP, hear for object of class dateinterval could not be converted to stringwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

### Calculate months between two dates using PHP:

in Pakainfo.com, PHP >=5.3 or more PHP Version we can use DateTime simple function used to diff that print and returns a DateInterval simple PHP object as below.

```\$first_date = new DateTime("2017-12-09");
\$second_date = new DateTime("2018-03-17");

var_dump(\$first_date->diff(\$second_date)->m); // int(3)
var_dump(\$first_date->diff(\$second_date)->m + (\$first_date->diff(\$second_date)->y*12)); // int(3)
```

in Pakainfo.com,,If we don’t have any PHP 5.3 or more PHP Version or higher, we can use simple PHP function strtotime() function to get timestamps as well as the number of seconds between any date and January 1 1970 00:00:00.

Read Also:  Calculate the difference between two dates using PHP

```\$first_date = "2017-12-09";
\$second_date = "2018-03-17";
echo (int)abs((strtotime(\$first_date) - strtotime(\$second_date))/(60*60*24*30)); // 3
```

The PHP ABS() function absolute value to repeatedly return a positive numeric number as the number of diff months between the two dates.

But it is not very special precise because there is not stuck repeatedly 30 days per month.

this can be an more option to get all the two dates months between two dates.

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

Other way to calculate the month difference between two dates.

```\$first_date = strtotime("2017-12-09");
\$second_date = strtotime("2018-03-17");
\$date_min = min(\$first_date, \$second_date);
\$date_max = max(\$first_date, \$second_date);
\$i = 0;

while ((\$date_min = strtotime("+1 MONTH", \$date_min)) <= \$date_max) {
\$i++;
}
echo \$i; // 3
```

If we want to include any remaining days like as a today or current month get we can add +1 to display the result.

Read Also:  PHP wordpress get timezone Example - MySQL

### Example 2 : Calculate days between two dates using PHP

Using DateTime for PHP >=5.3 or more PHP Version

```\$first_date = new DateTime("2017-12-09");
\$second_date = new DateTime("2018-03-17");
echo \$first_date->diff(\$second_date)->days; // 98
```

Note: But for windows 7 system there is a simple bug, It can always return error 6015 as a result for so \$first_date->diff(\$second_date)->days instead of 98.

For PHP < 5.3 or more PHP Version we can use simple timestamps to get all the days between two any dates using PHP simple strtotime() function.

```\$first_date = "2017-12-09";
\$second_date = "2018-03-17";

\$days = floor(abs(strtotime(\$second_date) - strtotime(\$first_date))/(60*60*24));
echo \$days. "Days";
```

We can use PHP simple below code also to two calculate days between dates result.

```\$first_date = strtotime("2017-12-09");
\$second_date = strtotime("2018-03-17");
\$date_min = min(\$first_date, \$second_date);
\$date_max = max(\$first_date, \$second_date);
\$i = 0;

while ((\$date_min = strtotime("+1 day", \$date_min)) <= \$date_max) {
\$i++;
}
echo \$i; // 98
```

Example

I hope you have Got What is date_diff() expects parameter 1 to be datetimeinterface 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.