Skip to content

Instantly share code, notes, and snippets.

@catrielmuller
Created March 14, 2019 10:45
Show Gist options
  • Save catrielmuller/58972df68e2621e568719da9a7911238 to your computer and use it in GitHub Desktop.
Save catrielmuller/58972df68e2621e568719da9a7911238 to your computer and use it in GitHub Desktop.
Get an Array of dates between 2 dates, one element per day
<?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