Created
March 14, 2019 10:45
-
-
Save catrielmuller/58972df68e2621e568719da9a7911238 to your computer and use it in GitHub Desktop.
Get an Array of dates between 2 dates, one element per day
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 | |
function diffDatesArray($from, $to, $formatInput, $formatOutput) { | |
$out = []; | |
$fromDate = DateTime::createFromFormat($formatInput, $from); | |
$toDate = DateTime::createFromFormat($formatInput, $to); | |
$interval = date_diff($fromDate, $toDate); | |
$daysInterval = intval($interval->format('%d')); | |
for ($i = 0; $i <= $daysInterval; $i++) { | |
$nextDay = clone $fromDate; | |
if($i >= 1){ | |
$nextDay->modify('+'.$i.' day'); | |
} | |
$out[] = $nextDay->format($formatOutput); | |
} | |
return $out; | |
} | |
$datesArray = diffDatesArray('20/03/2019', '3/04/2019', 'd/m/Y', 'd/m/Y'); | |
print_r($datesArray); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment