PHP code example of imranali / smart-timezone

1. Go to this page and download the library: Download imranali/smart-timezone 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/ */

    

imranali / smart-timezone example snippets


use ImranAli\SmartTimezone\Casts\TimezoneDateTime;

class Post extends Model
{
    protected $casts = [
        'published_at' => TimezoneDateTime::class,
        'created_at'   => TimezoneDateTime::class,
    ];
}

$post = Post::find(1);
echo $post->published_at; // "2026-06-25 18:45:00" for Lahore user
                          // DB still stores "2026-06-25 13:45:00" UTC

$post->published_at = now(); // Saves as UTC automatically

// For models loaded BEFORE login, use getDateInTimezone() for fresh conversion
$user = User::find(1);
// Auth::login($user);  // login happens after find
echo $user->getDateInTimezone('created_at'); // always uses latest timezone

use Carbon\Carbon;

$utcDate = Carbon::parse('2026-06-25 10:00:00', 'UTC');
$local = $utcDate->inUserTimezone(); // Macro added by package

echo $local->format('g:i A T'); // "3:00 PM PKT"

use ImranAli\SmartTimezone\Traits\HasTimezone;

class User extends Authenticatable
{
    use HasTimezone; // auto-syncs users.timezone column
}

auth()->user()->timezone; // "Asia/Karachi"
User::inTimezone('Asia/Karachi')->get(); // all Lahore users

Route::get('/posts', function () {
    return Post::all(); // published_at auto-converted to user TZ
});

$localCreatedAt = $post->created_at
    ->clone()
    ->setTimezone($user->timezone ?? config('smart-timezone.default'));

return [
    'default'            => env('APP_TIMEZONE', 'UTC'),
    'detect_from'        => ['header', 'cookie', 'user', 'ip'],
    'api_header'         => env('SMART_TIMEZONE_HEADER', 'X-Timezone'),

    'user_column'        => 'timezone',   // null = never touch the DB
    'auto_save_timezone' => true,         // persist a detected zone to the user

    'cookie_name'        => 'user_timezone',
    'cookie_lifetime'    => 31536000,
    'cookie_same_site'   => 'Lax',
    'cookie_secure'      => null,         // null = auto (Secure on HTTPS)

    'session_key'        => 'user_timezone',

    'register_middleware' => true,        // false = wire the middleware yourself
                                          // (aliases: timezone.detect / timezone.header)

    'enable_js_route'   => true,
    'route_prefix'      => '_smart-timezone',
    'route_middleware'  => ['web'],
    'route_throttle'    => '10,1',        // null to disable rate limiting
];

// bootstrap/app.php (Laravel 11+)
->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [\ImranAli\SmartTimezone\Middleware\DetectTimezone::class]);
    $middleware->api(append: [\ImranAli\SmartTimezone\Middleware\SetTimezoneFromHeader::class]);
})

Event::listen(function (TimezoneDetected $event) {
    if (auth()->check() && blank(auth()->user()->timezone)) {
        auth()->user()->forceFill(['timezone' => $event->timezone])->saveQuietly();
    }
});
bash
composer smart-timezone:install
php artisan migrate
bash
php artisan smart-timezone:install          # publish config + assets (+ migration prompt)
php artisan smart-timezone:install --force  # overwrite existing published files