PHP get difference of two given dates

To get the difference of the two given dates there is a custom made function in the php script share here you can pass two different dates and will get the difference of the date in the positive or negative numbers. It will help to determine that difference of the past dates or futures dates. Just copy this function and call this function passing the two dates as the parameters of this function.

<?php
// FUNCTION RETURN DAYS REMAINS OR DIFFERENCE OF TWO DATES
function days_remain($d1,$d2)
{
$date1=date_create(date("Y-m-d",strtotime($d1)));
$date2=date_create(date("Y-m-d",strtotime($d2)));
$diff=date_diff($date1,$date2);
// $diff->format("%R%a days");
// print_r($diff);
// return $diff->days;
return $diff->format("%R%a");
}

// CALL THE FUNCTION
$date_1 = "2016-10-10";
$date_2 = "2016-10-12";
echo days_remain($date_1,$date_2);
// OUTPUT "+2"


// CALL THE FUNCTION
$date_1 = "2015-11-09";
$date_2 = "2016-12-08";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "2021-03-07";
$date_2 = "2050-01-11";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "1988-08-30";
$date_2 = "1999-11-19";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "2005-09-25";
$date_2 = "2011-04-16";
echo days_remain($date_1,$date_2); 


// CALL THE FUNCTION
$date_1 = "1995-02-16";
$date_2 = "2009-09-22";
echo days_remain($date_1,$date_2); 

// CALL THE FUNCTION
$date_1 = "2001-04-21";
$date_2 = "2016-12-19";
echo days_remain($date_1,$date_2); 

// CALL THE FUNCTION
$date_1 = "2000-03-23";
$date_2 = "2025-08-29";
echo days_remain($date_1,$date_2); 
?>

Students Tech Life