Created
December 15, 2015 12:01
-
-
Save spivurno/3a652c00019a5be2c951 to your computer and use it in GitHub Desktop.
Calculate the difference in days between two dates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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