Created
April 9, 2019 08:26
-
-
Save chrismademe/52a28b65f69ab1191990bfebee9f2821 to your computer and use it in GitHub Desktop.
Get range of dates in an array
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 | |
/** | |
* Get Date Range | |
* | |
* @param string $start Start date in unix timestamp format | |
* @param string $end End date in unix timestamp format | |
* @return array Range of dates in an array | |
*/ | |
function get_date_range( $start, $end ) { | |
// Setup Dates | |
$start_date = date('Y-m-d', $start); | |
$end_date = date('Y-m-d', $end); | |
// Get date range | |
$period = new DatePeriod( | |
new DateTime($start_date), | |
new DateInterval('P1D'), | |
new DateTime($end_date) | |
); | |
// Add dates to an array | |
foreach ( $period as $value ) { | |
$the_dates[] = $value->format('Y-m-d'); | |
} | |
return ( is_array($the_dates) ? $the_dates : [] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment