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"

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

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

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('travel_itenery')
    ->startPlaceholder('Departure Date')

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

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

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

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

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')
    ->stacked() // Display inputs vertically stacked

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;
    })