Get list of all dates within a range in PHP

0
3234
PHP Programming Language
PHP Programming Language

Here is a tip to get all dates between two given dates.


$period = new \DatePeriod(
     new \DateTime($from_date),
     new \DateInterval('P1D'),
     new \DateTime($to_date)
);

The term P1D actually means plus 1 day, that means to increase 1 day from the previous day on the period loop. You can also use P1M and P1Y to indicate plus 1 month, and plus 1 year sequentially.

The return array is an array of DateTime objects, and you’re free to do anything with them.

Please refere to PHP DatePeriod class documentation for more info.