PHP code example of gnireeg / laravel-timezoned

1. Go to this page and download the library: Download gnireeg/laravel-timezoned 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/ */

    

gnireeg / laravel-timezoned example snippets


use Gnireeg\LaravelTimezoned\HasTimezoneConversion;

class Event extends Model
{
    use HasTimezoneConversion;

    protected array $timezonedAttributes = ['starts_at', 'ends_at'];
}

// Automatically converted to user's timezone
$event->starts_at; // Returns Carbon in user's timezone

// Automatically converted to database timezone
$event->starts_at = '2025-03-10 15:00';
$event->save();

// Get the value in database timezone (bypass conversion)
$event->getRawAttribute('starts_at');

// Default: uses authenticated user's timezone
'timezone_resolver' => function ($model) {
    return auth()->user()?->timezone ?? 'UTC';
},

// Or use session
'timezone_resolver' => fn() => session('timezone', 'UTC'),

// Or request header
'timezone_resolver' => fn() => request()->header('X-Timezone', 'UTC'),

'database_timezone' => null, // Uses app.timezone
'database_timezone' => 'UTC', // Explicit UTC
bash
php artisan vendor:publish --tag=config