Hundreds of founders, makers and marketers are part of The Digital Product Playbook.

This month we're giving away $700 of DigitalOcean credit, 1 year of SnapShooter along with a whole bunch of DigitalOcean goodies!

Join for free today at

Generate an Array of Dates Using Carbon

I find myself needing to loop over an array of dates quite often for forms at work. It's surprisingly easy thanks to Carbon.

Published 4 months ago

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\{


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.