Generate an Array of Dates Using Carbon

Table of Contents

When a customer at Surewise wants to take out a new insurance policy, they can only take one out with a start date within the next thirty days.

Date pickers are horrendnous with browser compatibility, given IE11 and Safari don't support them, so we instead choose to use a simple <select> element with the dates as <option> inside.

Here's how you can generate an array of dates for a given period:

use Carbon\{
    CarbonPeriod,
    Carbon
};

CarbonPeriod::create(
    Carbon::now(),
    Carbon::now()->addDays(30)
);

Calling this method will return an instance of Carbon\CarbonPeriod which has implements the Iterator interface and can therefore be used inside of a foreach statement.

When looping over the CarbonPeriod instance, the iteration variable will be an instance of Carbon\Carbon, so you can call all of the regular format and toDateTimeString() methods.

Enjoyed this post or found it useful? Please consider sharing it on Twitter.