PHP code example of codewithkyrian / filament-date-range

1. Go to this page and download the library: Download codewithkyrian/filament-date-range library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codewithkyrian / filament-date-range example snippets


use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;

DateRangePicker::make('event_period')
                ->label('Event Period'),

protected $casts = [
    'event_period' => 'json',
];

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;

protected function eventPeriod(): Attribute
{
    return Attribute::make(
        get: fn ($value, array $attributes) => [
            'start' => $attributes['starts_at'] ?? null,
            'end'   => $attributes['ends_at'] ?? null,
        ],
        set: fn (?array $value) => [
            'starts_at' => $value['start'] ? Carbon::parse($value['start']) : null,
            'ends_at'   => $value['end'] ? Carbon::parse($value['end']) : null,
        ],
    );
}

// To ensure the `event_period` virtual attribute is correctly handled during mass assignment operations (e.g., `Model::create([])` or `Model::update([])`),
// add its name to the `$fillable` array. This allows the `set` accessor to process the incoming date range.
protected $fillable = [
    // other fillables
    'event_period'
];


// To ensure your `event_period` attribute is available when Filament populates
// the form for an existing record, you may need to add it to the `$appends` array on your model:
protected $appends = [
    'event_period',
];

use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;

DateRangePicker::make('event_period')
    ->label('Event Period')

DateRangePicker::make('booking_dates')
    ->displayFormat('d/m/Y') // Displays as "15/01/2024"

DateRangePicker::make('log_period')
    ->format('Y-m-d H:i:s') // Stores as "2024-01-15 10:30:00"

use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;

DateRangePicker::configureUsing(function (DateRangePicker $picker) {
    $picker->defaultFormat('Y-m-d H:i:s');
});

use CodeWithKyrian\FilamentDateRange\Forms\Components\DateRangePicker;

DateRangePicker::configureUsing(function (DateRangePicker $picker) {
    $picker->defaultDisplayFormat('d/m/Y');
});

DateRangePicker::make('future_event')
    ->minDate(now()->addDay())

DateRangePicker::make('past_event')
    ->maxDate(now()->subDay())

DateRangePicker::make('available_dates')
    ->enabledDates([
        '2024-01-15',
        '2024-01-16', 
        '2024-01-20',
        now()->addDays(5),
    ])

DateRangePicker::make('période')
    ->locale('fr')

DateRangePicker::make('appointment_time')
    ->timezone('America/New_York')

DateRangePicker::make('work_schedule')
    ->firstDayOfWeek(1) // Week starts on Monday

DateRangePicker::make('work_schedule')
    ->weekStartsOnMonday()

DateRangePicker::make('work_schedule')
    ->weekStartsOnSunday()

DateRangePicker::make('maintenance_window')
    ->withTime()
    ->helperText('Defaults to all-day; toggle off to pick specific times.');

DateRangePicker::make('maintenance_window')
    ->withTime()
    ->allDay(true, true);

DateRangePicker::make('travel_itenery')
    ->startPlaceholder('Departure Date')

DateRangePicker::make('travel_itenery')
    ->endPlaceholder('Return Date')

DateRangePicker::make('duration')
    ->separator('through')

DateRangePicker::make('duration')
    ->stacked(['default' => true, 'lg' => false])
    ->separator([
        'stacked' => '↓',
        'inline' => '→',
    ])

DateRangePicker::make('project_duration')
    ->separatorIcon('heroicon-o-arrow-long-right')

DateRangePicker::make('project_duration')
    ->stacked(['default' => true, 'lg' => false])
    ->separatorIcon([
        'stacked' => 'heroicon-o-arrow-down',
        'inline' => 'heroicon-o-arrow-long-right',
    ])

DateRangePicker::make('conference_dates')
    ->autoApply(true)

DateRangePicker::make('order_period')
    ->label('Order Period')
    ->presets(); // use built-in presets

DateRangePicker::make('order_period_custom')
    ->label('Order Period')
    ->presets([
        // Just keys (uses built-in labels)
        'last_7_days',
        'last_14_days',

        // Or explicit key + custom label
        ['key' => 'this_month', 'label' => 'Current Month'],
    ]);

DateRangePicker::make('date_range')
    ->dualCalendar(false) // Show only a single month calendar

DateRangePicker::make('event_period')
    ->inline() // Display inputs side by side (default)

DateRangePicker::make('event_period')
    ->inline(['default' => false, 'lg' => true]) // Stacked on mobile, inline from lg

DateRangePicker::make('event_period')
    ->stacked() // Display inputs vertically stacked

DateRangePicker::make('event_period')
    ->stacked(['default' => true, 'lg' => false]) // Stacked on mobile, inline from lg

DateRangePicker::make('invoice_period')
    ->label('Invoice Period')
    ->singleField();

use Filament\Forms\Components\Actions\Action;

DateRangePicker::make('licence_validity')
    ->startPrefix('Valid')
    ->endSuffixAction(
        Action::make('info')
            ->icon('heroicon-o-information-circle')
            ->tooltip('This range is inclusive.')
            ->action(fn () => null)
    )

use CodeWithKyrian\FilamentDateRange\Tables\Filters\DateRangeFilter;

DateRangeFilter::make('created_at'
            ->label('Created within'),

DateRangeFilter::make('processed_at')
    ->label('Processing Date')
    ->displayFormat('d M Y')
    ->minDate(now()->subYear())

DateRangeFilter::make('event_span')
    ->label('Event Overlaps With')
    ->modifyQueryUsing(function (Builder $query, array $data, $state) {
        $start = $state['start'] ? Carbon::parse($state['start'])->startOfDay() : null;
        $end = $state['end'] ? Carbon::parse($state['end'])->endOfDay() : null;

        if ($start && $end) {
            return $query->where(function (Builder $query) use ($start, $end) {
                $query->where('event_starts_at', '<=', $start)
                        ->where('event_ends_at', '>=', $end);
            });
        }
        return $query;
    })