How To Get Difference Between Two Dates In PHP?

In this code model we are taking two dates and getting their disparities, following code can be utilized to get definite date distinction in days between two dates.

We may at some point require to get a contrast between in PHP for your application, use any PHP framework like Laravel, Codeigniter, Wordpress but you can use code PHP code anywhere. So you can see the following examples.

Bellow examples give you to calculate the difference between two dates in PHP example.

Example 1
$startDate = "2018-01-28";
$endDate = "2020-08-19";

$second1year = 365*60*60*24;
$second1month = 30*60*60*24;
$second1day = 60*60*24;

$diffData = abs(strtotime($endDate) - strtotime($startDate));

$yearsDiff = floor($diffData / $second1year); 
echo ("Years:".$yearsDiff);
$monthsDiff = floor(($diffData - $yearsDiff * $second1year) / ($second1month));
echo (" Months:".$monthsDiff);
$daysDiff = floor(($diffData - $yearsDiff * $second1year - $monthsDiff* $second1month)/ ($second1day));
echo (" Days:".$daysDiff);

How To Get [Difference Between Two Dates In PHP ?] - Php Coding Stuff
Example 2

	function dateDiff($date1, $date2) 
	  $date1_new = strtotime($date1);
	  $date2_new = strtotime($date2);
	  $diff = $date2_new - $date1_new;
	  return round($diff / 86400);    //60*60*24 (1day second)
	$dateDiff= dateDiff($date1, $date2);
	printf("Difference between in two dates : " .$dateDiff. " Days ");
	print "</br>";
How To Get [Difference Between Two Dates In PHP ?] - Php Coding Stuff

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!