Generate Intervals of Time With the Laravel Hours Helper

Packages

January 7th, 2022

Generate Intervals of Time With the Laravel Hours Helper

With the Laravel Hours Helper package, you can create a collection of dates and/of times with a specific interval (in minutes) for a particular period.

1use Facades\Label84\HoursHelper\HoursHelper;
2 
3$hours = HoursHelper::create('08:00', '09:30', 30);
4/*
5=> Illuminate\Support\Collection {#3529
6 all: [
7 "08:00",
8 "08:30",
9 "09:00",
10 "09:30",
11 ],
12 }
13*/

This helper could be useful in generating dropdown selections for a calendar meeting invite or scheduling the duration of an event. This helper also allows you to define the date formatting for each interval of time:

1$hours = HoursHelper::create('11:00', '13:00', 60, 'g:i A');
2/*
3Illuminate\Support\Collection {#3528
4 all: [
5 "11:00 AM",
6 "12:00 PM",
7 "1:00 PM",
8 ],
9}
10*/

Further, this helper can deal with intervals past midnight (i.e., the next day) and over multiple days:

1HoursHelper::create(
2 '2021-01-01 22:00',
3 '2021-01-03 13:00',
4 60,
5 'Y-m-d H:i'
6);
7/*
8Illuminate\Support\Collection {#3566
9 all: [
10 "2021-01-01 22:00",
11 "2021-01-01 23:00",
12 "2021-01-02 00:00",
13 "2021-01-02 01:00",
14 "2021-01-02 02:00",
15 ...
16 ]
17}
18*/

You can learn more about this package, get full installation instructions, and view the source code on GitHub.


This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter at @LaravelLinks

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.