Skip to content

Instantly share code, notes, and snippets.

@fawaztahir
Created August 18, 2023 12:43
Show Gist options
  • Select an option

  • Save fawaztahir/8097f747da28454a74271ffa9af83898 to your computer and use it in GitHub Desktop.

Select an option

Save fawaztahir/8097f747da28454a74271ffa9af83898 to your computer and use it in GitHub Desktop.
Create date range in PHP
<?php
function createDateRange(string $start, string $end, string $format = 'Y-m-d'): array
{
$range = [];
$interval = new DateInterval('P1D');
$endDT = new DateTime($end);
$endDT->add($interval);
$period = new DatePeriod(new DateTime($start), $interval, $endDT);
foreach($period as $date) {
array_push($range, $date->format($format));
}
return $range;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment