PHP code example of athwari / laravel-user-timezone

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

    

athwari / laravel-user-timezone example snippets


// config/user-timezone.php
return [
    'user_model' => env('USER_TIMEZONE_MODEL', 'App\\Models\\User'),
    'column' => env('USER_TIMEZONE_COLUMN', 'timezone'),
    'fallback' => env('USER_TIMEZONE_FALLBACK', config('app.timezone')),
    'middleware' => [
        'enabled' => env('USER_TIMEZONE_MIDDLEWARE_ENABLED', true),
        'set_php_timezone' => env('USER_TIMEZONE_SET_PHP_TIMEZONE', true),
    ],
];

use Athwari\LaravelUserTimezone\Facades\UserTimezone;

UserTimezone::get();                          // 'Europe/London'
UserTimezone::resolveFromUser($user);         // 'Europe/London' or fallback
UserTimezone::set('America/New_York');        // override for request
UserTimezone::clear();                        // remove override
UserTimezone::now();                          // Carbon in user's timezone
UserTimezone::convert($post->created_at);     // Carbon converted to user's timezone
UserTimezone::isValid('Invalid/Zone');        // false

user_timezone(); // string

use Athwari\LaravelUserTimezone\Traits\HasTimezone;

class User extends Authenticatable
{
    use HasTimezone;
}

$user->getTimezone();              // 'Europe/London' or fallback
$user->setTimezone('Asia/Tokyo');  // validates and sets
$user->timezone = 'UTC';           // validated attribute mutator

use Athwari\LaravelUserTimezone\Traits\ConvertsDatesToUserTimezone;

class Post extends Model
{
    use ConvertsDatesToUserTimezone;
}

$post->inUserTimezone('created_at'); // Carbon in user's timezone
bash
php artisan vendor:publish --tag=user-timezone-config
php artisan vendor:publish --tag=user-timezone-migrations
php artisan migrate