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