Skip to content

Instantly share code, notes, and snippets.

@spivurno
Created December 15, 2015 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spivurno/3a652c00019a5be2c951 to your computer and use it in GitHub Desktop.
Save spivurno/3a652c00019a5be2c951 to your computer and use it in GitHub Desktop.
Calculate the difference in days between two dates.
<?php
/**
* Calculate the difference in days between two dates.
*
* @param string $date1 Provide in 'YYYY-MM-DD' format.
* @param bool $date2 Provide in 'YYYY-MM-DD' format. If no date provided, current date will be used.
*
* @return integer Difference in days between the two dates.
*/
function gw_day_diff( $date1, $date2 = false ) {
if( ! $date2 ) {
$date2 = date( 'Y-m-d' );
}
$date1 = new DateTime( sprintf( '%s 00:00:00', $date1 ) );
$date2 = new DateTime( sprintf( '%s 00:00:00', $date2 ) );
$diff = $date1->diff( $date2 );
return max( $diff->d, 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment