Created
August 18, 2023 12:43
-
-
Save fawaztahir/8097f747da28454a74271ffa9af83898 to your computer and use it in GitHub Desktop.
Create date range in PHP
This file contains hidden or 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 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