+

PHP Date_diff () function

Syntax:
date_diff ($datetime1, $datetime2); 
Parameters:The date_diff() function takes two parameters as above and described below:
  • $datetime1: this is a required parameter that specifies the first DateTime object.
  • $datetime2:this is a required parameter that specifies the second DateTime object.
Return value:otherwise returns the difference between the two DateTime objects, FALSE on error.The programs below illustrate the date_diff() function:
Program 1:
// PHP illustration program
// date_diff() function 
// creates DateTime objects $datetime1 = date_create ( `2017-06-28` ); $datetime2 = date_create ( `2018 -06-28` );  
// calculate the difference between DateTime objects $interval = date_diff ( $datetime1 , $datetime2 );  
// result output in days format echo $interval -> format ( `% R% a days` ); ?>
Exit:
+365 days 
Program 2:
// PHP illustration program
// date_diff() function 
// the difference is only in the year $datetime1 = date_create ( `2017-06-28` ); $datetime2 = date_create ( `2018 -06-28` ); $interval = date_diff ( $datetime1 , $datetime2 ); echo $interval -> format ( `% R% a days` ). "" ;  
// The difference is only in months $datetime1 = date_create ( `2018-04-28` ); $datetime2 = date_create ( `2018 -06-28` ); $interval = date_diff ( $datetime1 , $datetime2 ); echo $interval -> format ( `% R% a days` ). "" ;  
// Difference in year, month, days $datetime1 = date_create ( `2017-06-28` ); $datetime2 = date_create ( `2018 -04-05` ); $interval = date_diff ( $datetime1 , $datetime2 ); echo $interval -> format ( `% R% a days` ). "" ;  
?>
Exit:
+365 days +61 days +281 days 
Link : http://php.net/manual/en/function.date-diff.php
Get Solution for free from DataCamp guru