PHP Calculate Date Difference in Days Example

PHP

Nicesnippets

233

11-02-2020


This article will give you example of php calculate date difference. step by step explain php calculate date add days. i would like to share with you date add days in php. This tutorial will give you simple example of how to calculate date difference in days php. You just need to some step to done calculate date difference in days in php.

Example 1:

In this example, first take two dates and get their differences.you can bellow this example.

<?php

function dateDiffInDays($date1, $date2)

{

$diff = strtotime($date2) - strtotime($date1);

return abs(round($diff / 86400));

}

$date1 = "15-01-2020";

$date2 = "25-01-2020";

$dateDiff = dateDiffInDays($date1, $date2);

printf("Calculate Date Difference in Days: "

. $dateDiff . " Days ");

?>

Output :

Calculate Date Difference in Days: 10 Days

Example 2:

The date_diff() function is an inbuilt function in PHP which is used to calculate the difference between two dates.

<?php

$datetime1 = date_create('17-01-2020');

$datetime2 = date_create('24-01-2020');

$interval = date_diff($datetime1, $datetime2);

echo $interval->format('Calculate Date Difference in Days: %R%a days');

?>

Output :

Calculate Date Difference in Days: +7 days

It will help you....


Recommended Posts