label84/laravel-hours-helper
Generate Laravel collections of time/date intervals for any period: build dropdown-ready schedules with custom formatting, exclusions, support for past-midnight ranges, and multi-day spans. Simple facade API to create evenly spaced slots like 08:00–09:30 every 30 minutes.
Strengths:
Collection and Carbon paradigms, reducing friction in integration. The facade pattern (HoursHelper) simplifies usage in Blade, controllers, and services.Collection return type enables seamless chaining with Laravel’s query builder or Eloquent.Weaknesses:
[09:00, 09:59]), lacking conflict detection with external systems (e.g., database bookings). Requires manual integration with Eloquent or Query Builder.CarbonTimeZone logic, adding complexity.Stack Compatibility:
Collection, Carbon, and service container. Works seamlessly with Blade, APIs, and Queues.Collection objects that can be:
<option>{{ $slot }}</option>).bookings table and pass to HoursHelper::create().DB::select() to get blocked ranges and merge with exclusions.Rule::in($validSlots)) for form inputs.Dependencies:
carbon/carbon (bundled with Laravel).Low Risk:
HoursHelper::create('08:00', '17:00', 30) generates a Collection in one line.Mitigable Risks:
[09:00, 10:00] and [09:30, 10:30]) may need normalization. Solution: Pre-process exclusions into non-overlapping ranges.Carbon instances in CarbonTimeZone before passing to the helper.Laravel Integration Points:
<select name="appointment_time">
@foreach ($availableSlots as $slot)
<option>{{ $slot }}</option>
@endforeach
</select>
return response()->json($hours->toArray());
'time_slot' => ['required', Rule::in($validSlots->pluck('value')->toArray())],
foreach ($hours as $slot) {
SendReminderJob::dispatch($slot);
}
$excludedRanges = DB::table('bookings')->pluck('time_range');
$hours = HoursHelper::create('08:00', '17:00', 30, 'H:i', $excludedRanges);
Frontend Integration:
// Alpine.js example
<select x-model="selectedTime" wire:model="appointment_time">
@foreach ($availableSlots as $slot)
<option>{{ $slot }}</option>
@endforeach
</select>
$slots = HoursHelper::create('09:00', '17:00', 60)->toArray();
return view('calendar', compact('slots'));
Database Integration:
$blockedRanges = DB::table('blocked_times')
->where('date', $date)
->pluck('range')
->toArray();
$availableSlots = HoursHelper::create('08:00', '18:00', 30, 'H:i', $blockedRanges);
scheduling_slots table if pre-computing is beneficial.
Cache::remember("slots_{$date}", now()->addDay(), function () use ($date) {
return HoursHelper::create($date.' 08:00', $date.' 18:00', 30)->toArray();
});
How can I help you explore Laravel packages today?