├── .github └── workflows │ ├── phpstan.yml │ ├── pint.yml │ └── run-tests.yml ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpstan.neon.dist ├── phpunit.xml ├── src ├── Facades │ └── HoursHelper.php ├── HoursHelper.php └── HoursHelperServiceProvider.php └── tests ├── HoursHelperTest.php └── TestCase.php /.github/workflows/phpstan.yml: -------------------------------------------------------------------------------- 1 | name: Run PHPStan 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | phpstan: 11 | name: phpstan 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v4 15 | 16 | - name: Setup PHP 17 | uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: '8.2' 20 | coverage: none 21 | 22 | - name: Install composer dependencies 23 | uses: ramsey/composer-install@v2 24 | 25 | - name: Run PHPStan 26 | run: ./vendor/bin/phpstan --error-format=github 27 | -------------------------------------------------------------------------------- /.github/workflows/pint.yml: -------------------------------------------------------------------------------- 1 | name: Run Pint 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | pint: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: Checkout code 15 | uses: actions/checkout@v4 16 | with: 17 | ref: ${{ github.head_ref }} 18 | 19 | - name: Run Pint 20 | uses: aglipanci/laravel-pint-action@2.3.0 21 | 22 | - name: Commit changes 23 | uses: stefanzweifel/git-auto-commit-action@v5 24 | with: 25 | commit_message: Fix styling 26 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: Run tests 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | test: 13 | runs-on: ${{ matrix.os }} 14 | 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | include: 19 | - php: 8.2 20 | laravel: '11.*' 21 | testbench: '9.*' 22 | larastan: '2.*' 23 | dependency-version: prefer-stable 24 | os: ubuntu-latest 25 | - php: 8.2 26 | laravel: '12.*' 27 | testbench: '10.*' 28 | larastan: '3.1.*' 29 | dependency-version: prefer-stable 30 | os: ubuntu-latest 31 | 32 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 33 | 34 | steps: 35 | - name: Checkout code 36 | uses: actions/checkout@v4 37 | 38 | - name: Cache dependencies 39 | uses: actions/cache@v4 40 | with: 41 | path: ~/.composer/cache/files 42 | key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 43 | 44 | - name: Setup PHP 45 | uses: shivammathur/setup-php@v2 46 | with: 47 | php-version: ${{ matrix.php }} 48 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 49 | coverage: none 50 | 51 | - name: Install dependencies 52 | run: | 53 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 54 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction 55 | 56 | - name: Execute tests 57 | run: vendor/bin/phpunit 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .php_cs.cache 5 | .DS_Store 6 | /.idea 7 | /.vscode 8 | .phpunit.result.cache 9 | .php-cs-fixer.cache -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Label84 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | [MIT License](https://opensource.org/licenses/MIT) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Hours Helper 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/label84/laravel-hours-helper/v/stable?style=flat-square)](https://packagist.org/packages/label84/laravel-hours-helper) 4 | [![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/label84/laravel-hours-helper.svg?style=flat-square)](https://packagist.org/packages/label84/laravel-hours-helper) 6 | ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/label84/laravel-hours-helper/run-tests.yml?branch=master&style=flat-square) 7 | 8 | With ``laravel-hours-helper`` you can create an ``Illuminate\Support\Collection`` of dates and/or times with a specific interval for a specific period. 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 and to exclude intervals within the specific period. 9 | 10 | - [Laravel Support](#laravel-support) 11 | - [Installation](#installation) 12 | - [Usage](#usage) 13 | - [Tests](#tests) 14 | - [License](#license) 15 | 16 | ## Laravel Support 17 | 18 | | Version | Release | 19 | |---------|---------| 20 | | 12.x | ^1.4 | 21 | | 11.x | ^1.4 | 22 | 23 | ## Installation 24 | 25 | Install the package via composer: 26 | 27 | ```sh 28 | composer require label84/laravel-hours-helper 29 | ``` 30 | 31 | ## Usage 32 | 33 | ```php 34 | use Label84\HoursHelper\Facades\HoursHelper; 35 | 36 | $hours = HoursHelper::create('08:00', '09:30', 30); 37 | 38 | // Illuminate\Support\Collection 39 | 0 => '08:00', 40 | 1 => '08:30', 41 | 2 => '09:00', 42 | 3 => '09:30', 43 | ``` 44 | 45 | ### Example 1: time format 46 | 47 | ```php 48 | use Label84\HoursHelper\Facades\HoursHelper; 49 | 50 | $hours = HoursHelper::create('11:00', '13:00', 60, 'g:i A'); 51 | 52 | // Illuminate\Support\Collection 53 | 0 => '11:00 AM', 54 | 1 => '12:00 PM', 55 | 2 => '1:00 PM', 56 | ``` 57 | 58 | ### Example 2: exclude times 59 | 60 | ```php 61 | use Label84\HoursHelper\Facades\HoursHelper; 62 | 63 | $hours = HoursHelper::create('08:00', '11:00', 60, 'H:i', [ 64 | ['09:00', '09:59'], 65 | // more.. 66 | ]); 67 | 68 | // Illuminate\Support\Collection 69 | 0 => '08:00', 70 | 1 => '10:00', 71 | 2 => '11:00', 72 | ``` 73 | 74 | ### Example 3: past midnight 75 | 76 | ```php 77 | use Label84\HoursHelper\Facades\HoursHelper; 78 | 79 | $hours = HoursHelper::create('23:00', '01:00', 60); 80 | 81 | // Illuminate\Support\Collection 82 | 0 => '23:00', 83 | 1 => '00:00', 84 | 2 => '01:00', 85 | ``` 86 | 87 | ### Example 4: multiple days 88 | 89 | ```php 90 | use Label84\HoursHelper\Facades\HoursHelper; 91 | 92 | $hours = HoursHelper::create('2022-01-01 08:00', '2022-01-01 08:30', 15, 'Y-m-d H:i'); 93 | 94 | // Illuminate\Support\Collection 95 | 0 => '2022-01-01 08:00', 96 | 1 => '2022-01-01 08:15', 97 | 2 => '2022-01-01 08:30', 98 | ``` 99 | 100 | You can find more examples in the test directory: [tests/HoursHelperTest.php](tests/HoursHelperTest.php) 101 | 102 | ## Tests 103 | 104 | ```sh 105 | ./vendor/bin/phpstan analyze 106 | ./vendor/bin/phpunit 107 | ``` 108 | 109 | ## License 110 | 111 | [MIT](https://opensource.org/licenses/MIT) 112 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "label84/laravel-hours-helper", 3 | "description": "Creates a Collection of times with a given interval.", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Label84", 9 | "email": "info@label84.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^8.2", 14 | "illuminate/support": "^11.0|^12.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "Label84\\HoursHelper\\": "src" 19 | } 20 | }, 21 | "autoload-dev": { 22 | "psr-4": { 23 | "Label84\\HoursHelper\\Tests\\": "tests" 24 | } 25 | }, 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "Label84\\HoursHelper\\HoursHelperServiceProvider" 30 | ], 31 | "aliases": { 32 | "HoursHelper": "Label84\\HoursHelper\\Facades\\HoursHelper" 33 | } 34 | } 35 | }, 36 | "require-dev": { 37 | "orchestra/testbench": "^9.0|^10.0", 38 | "larastan/larastan": "^2.0|^3.1", 39 | "phpunit/phpunit": "^11.5|^12", 40 | "laravel/pint": "^1.21" 41 | }, 42 | "minimum-stability": "dev", 43 | "prefer-stable": true 44 | } 45 | -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - ./vendor/larastan/larastan/extension.neon 3 | 4 | parameters: 5 | 6 | paths: 7 | - src 8 | 9 | level: 8 10 | 11 | ignoreErrors: 12 | - identifier: missingType.iterableValue 13 | - identifier: missingType.generics 14 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests 6 | 7 | 8 | 9 | 10 | src/ 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/Facades/HoursHelper.php: -------------------------------------------------------------------------------- 1 | isSameDay($end) && $end->isBefore($start)) { 24 | $end = $end->addDay(); 25 | } 26 | 27 | $period = CarbonInterval::minutes($interval)->toPeriod($start, $end); 28 | 29 | /** @phpstan-ignore-next-line */ 30 | return collect($period) 31 | ->reject(function (Carbon $carbon) use ($excludes) { 32 | foreach ($excludes as $exclude) { 33 | $start = Carbon::parse($exclude[0]); 34 | $end = Carbon::parse($exclude[1]); 35 | 36 | if ($start->gt($end)) { 37 | $end->addDay(); 38 | } 39 | 40 | if ($start->isToday() && $end->isToday() && $carbon->isTomorrow()) { 41 | $start->addDay(); 42 | $end->addDay(); 43 | } 44 | 45 | if ($carbon->between($start, $end)) { 46 | return true; 47 | } 48 | } 49 | 50 | return false; 51 | }) 52 | ->map->format($format) 53 | ->values(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/HoursHelperServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind(HoursHelper::class, function ($app) { 12 | return new HoursHelper; 13 | }); 14 | } 15 | 16 | public function boot(): void 17 | { 18 | // 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tests/HoursHelperTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($collection, $result); 24 | } 25 | 26 | public function test_it_can_have_the_start_and_end_value_in_datetime_format() 27 | { 28 | $start = Carbon::createFromFormat('H:i', '08:00'); 29 | $end = Carbon::createFromFormat('H:i', '09:00'); 30 | 31 | $result = HoursHelper::create($start, $end, 15); 32 | 33 | $collection = new Collection([ 34 | '08:00', 35 | '08:15', 36 | '08:30', 37 | '08:45', 38 | '09:00', 39 | ]); 40 | 41 | $this->assertEquals($collection, $result); 42 | } 43 | 44 | public function test_it_can_have_an_end_time_later_than_midnight() 45 | { 46 | $result = HoursHelper::create('23:45', '00:15', 15); 47 | 48 | $collection = new Collection([ 49 | '23:45', 50 | '00:00', 51 | '00:15', 52 | ]); 53 | 54 | $this->assertEquals($collection, $result); 55 | } 56 | 57 | public function test_it_uses_the_h_i_formatter_by_default() 58 | { 59 | $result = HoursHelper::create('08:00', '08:30', 30, 'H:i'); 60 | 61 | $collection = new Collection([ 62 | '08:00', 63 | '08:30', 64 | ]); 65 | 66 | $this->assertEquals($collection, $result); 67 | } 68 | 69 | public function test_it_can_have_dates_in_the_formatter() 70 | { 71 | $result = HoursHelper::create('2022-01-01 08:00', '2022-01-01 08:30', 15, 'Y-m-d H:i'); 72 | 73 | $collection = new Collection([ 74 | '2022-01-01 08:00', 75 | '2022-01-01 08:15', 76 | '2022-01-01 08:30', 77 | ]); 78 | 79 | $this->assertEquals($collection, $result); 80 | } 81 | 82 | public function test_it_can_have_the_12_hour_format_in_the_formatter() 83 | { 84 | $result = HoursHelper::create('11:00', '13:00', 60, 'g:i A'); 85 | 86 | $collection = new Collection([ 87 | '11:00 AM', 88 | '12:00 PM', 89 | '1:00 PM', 90 | ]); 91 | 92 | $this->assertEquals($collection, $result); 93 | } 94 | 95 | public function test_it_can_have_an_interval_of_multiple_days() 96 | { 97 | $result = HoursHelper::create('2022-01-01', '2022-01-04', 60 * 24 * 1.5, 'Y-m-d H:i'); 98 | 99 | $collection = new Collection([ 100 | '2022-01-01 00:00', 101 | '2022-01-02 12:00', 102 | '2022-01-04 00:00', 103 | ]); 104 | 105 | $this->assertEquals($collection, $result); 106 | } 107 | 108 | public function test_it_can_exclude_times() 109 | { 110 | $result = HoursHelper::create('08:30', '10:00', 15, 'H:i', [ 111 | ['09:00', '09:30'], 112 | ]); 113 | 114 | $collection = new Collection([ 115 | '08:30', 116 | '08:45', 117 | '09:45', 118 | '10:00', 119 | ]); 120 | 121 | $this->assertEquals($collection, $result); 122 | } 123 | 124 | public function test_it_can_exclude_dates() 125 | { 126 | $result = HoursHelper::create('2021-01-01 23:30', '2021-01-03 00:30', 15, 'Y-m-d H:i', [ 127 | ['2021-01-02 00:00', '2021-01-02 23:59'], 128 | ]); 129 | 130 | $collection = new Collection([ 131 | '2021-01-01 23:30', 132 | '2021-01-01 23:45', 133 | '2021-01-03 00:00', 134 | '2021-01-03 00:15', 135 | '2021-01-03 00:30', 136 | ]); 137 | 138 | $this->assertEquals($collection, $result); 139 | } 140 | 141 | public function test_it_can_exclude_multiple_time_intervals() 142 | { 143 | $result = HoursHelper::create('08:00', '13:00', 60, 'H:i', [ 144 | ['09:00', '09:59'], 145 | ['11:00', '11:59'], 146 | ]); 147 | 148 | $collection = new Collection([ 149 | '08:00', 150 | '10:00', 151 | '12:00', 152 | '13:00', 153 | ]); 154 | 155 | $this->assertEquals($collection, $result); 156 | } 157 | 158 | public function test_it_can_exclude_time_interval_that_is_past_midnight() 159 | { 160 | $result = HoursHelper::create('22:00', '03:00', 60, 'H:i', [ 161 | ['01:00', '02:00'], 162 | ]); 163 | 164 | $collection = new Collection([ 165 | '22:00', 166 | '23:00', 167 | '00:00', 168 | '03:00', 169 | ]); 170 | 171 | $this->assertEquals($collection, $result); 172 | } 173 | 174 | public function test_it_can_exclude_time_intervals_that_are_before_midnight_and_past_midnight() 175 | { 176 | $result = HoursHelper::create('18:00', '05:00', 60, 'H:i', [ 177 | ['21:00', '23:00'], 178 | ['01:00', '02:00'], 179 | ]); 180 | 181 | $collection = new Collection([ 182 | '18:00', 183 | '19:00', 184 | '20:00', 185 | '00:00', 186 | '03:00', 187 | '04:00', 188 | '05:00', 189 | ]); 190 | 191 | $this->assertEquals($collection, $result); 192 | } 193 | 194 | public function test_it_can_exclude_time_interval_including_midnight() 195 | { 196 | $result = HoursHelper::create('23:00', '00:30', 15, 'H:i', [ 197 | ['23:45', '00:14'], 198 | ]); 199 | 200 | $collection = new Collection([ 201 | '23:00', 202 | '23:15', 203 | '23:30', 204 | '00:15', 205 | '00:30', 206 | ]); 207 | 208 | $this->assertEquals($collection, $result); 209 | } 210 | 211 | public function test_it_can_exclude_dates_that_are_during_midnight() 212 | { 213 | $result = HoursHelper::create('2021-01-01 23:30', '2021-01-03 01:00', 15, 'Y-m-d H:i', [ 214 | ['2021-01-01 23:45', '2021-01-03 00:30'], 215 | ]); 216 | 217 | $collection = new Collection([ 218 | '2021-01-01 23:30', 219 | '2021-01-03 00:45', 220 | '2021-01-03 01:00', 221 | ]); 222 | 223 | $this->assertEquals($collection, $result); 224 | } 225 | 226 | public function test_it_can_exclude_dates_that_are_on_midnight() 227 | { 228 | $result = HoursHelper::create('2023-12-19 23:30', '2023-12-21 01:00', 15, 'Y-m-d H:i', [ 229 | ['2023-12-20 00:00', '2023-12-21 00:30'], 230 | ]); 231 | 232 | $collection = new Collection([ 233 | '2023-12-19 23:30', 234 | '2023-12-19 23:45', 235 | '2023-12-21 00:45', 236 | '2023-12-21 01:00', 237 | ]); 238 | 239 | $this->assertEquals($collection, $result); 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 |