Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Carbon, get all dates between date range?

Tags:

php

php-carbon

How can I get all dates between two dates in PHP? Prefer using Carbon for dates.

$from = Carbon::now(); $to = Carbon::createFromDate(2017, 5, 21); 

I wanna have all dates between those two dates.. But how? Can only found solutions using strtotime function.

like image 705
user1469734 Avatar asked Aug 06 '15 07:08

user1469734


1 Answers

As of Carbon 1.29 it is possible to do:

$period = CarbonPeriod::create('2018-06-14', '2018-06-20');  // Iterate over the period foreach ($period as $date) {     echo $date->format('Y-m-d'); }  // Convert the period to an array of dates $dates = $period->toArray(); 

See documentation for more details: https://carbon.nesbot.com/docs/#api-period.

like image 53
Paul Avatar answered Oct 10 '22 11:10

Paul