Download the PHP package imranali/smart-timezone without Composer
On this page you can find all versions of the php package imranali/smart-timezone. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download imranali/smart-timezone
More information about imranali/smart-timezone
Files in imranali/smart-timezone
Package smart-timezone
Short Description Automatic timezone detection and casting for Laravel - auto-detect user timezone from JS, header, cookie, user profile, or IP, and seamlessly cast all dates to local time.
License MIT
Informations about the package smart-timezone
SmartTimezone for Laravel
Stop timezone bugs forever. SmartTimezone auto-detects your users' timezone and makes every datetime "just work" in their local time.
No more ->setTimezone('UTC') spaghetti. No more "why does this show 5 hours off?"
What It Does
- Auto-detects timezone — from browser JS → cookie → user profile → IP → fallback
- Auto-casts dates —
$post->created_atshows in user timezone, saves as UTC - Blade ready —
@usertime($datetime)prints "25 Jun 2026, 6:45 PM PKT" - API friendly — Reads
X-Timezoneheader for mobile/SPA apps - Zero config — works for 80% of apps. Supports Laravel 10, 11, 12, 13.
Installation
Make sure your layout <head> has a CSRF token, then add the script before </body>:
The script sets a user_timezone cookie and (when the CSRF token is present)
pushes the detected zone to the current session immediately. Without the meta
tag it still works — the server just picks the zone up on the next request from
the cookie.
That's it. All dates now respect user timezone.
Usage
1. Cast any datetime automatically
2. Blade directive
3. Manual conversion
4. Add to User model
5. API / Mobile App Usage
Send the timezone header from your frontend. SmartTimezone picks it up automatically.
Axios / React
Flutter / Dart
Laravel API response:
Response for a Lahore user:
How Detection Works
When rendering a date, SmartTimezone resolves the timezone in this order:
| Priority | Source | When it's used |
|---|---|---|
| 1 | X-Timezone header |
API / mobile / SPA requests — overrides everything for that request |
| 2 | users.timezone DB |
Authenticated users — the source of truth once logged in |
| 3 | Session | Guests — populated by the middleware from the cookie or IP lookup |
| 4 | Model's own timezone |
User-owned rows (e.g. a Post belonging to a user) |
| 5 | config('smart-timezone.default') (APP_TIMEZONE) |
Nothing else matched |
The DetectTimezone middleware fills the session once per session for
guests, trying detect_from sources in order (header, cookie, user, ip).
The user_timezone cookie is set client-side by timezone-detect.js.
Key behaviors
- Header wins for API/SPA requests and is never persisted to the session.
- DB column wins for logged-in users — no "one request is wrong after login" race, because resolution reads the user's column directly.
- Every value is validated against
timezone_identifiers_list(). Offset-only values like+05:00are not IANA identifiers and are ignored — sendAsia/Karachi, not an offset. - Eloquent caches cast values. If a model was read before
Auth::login()and you need the post-login zone in the same request, use$model->getDateInTimezone('created_at')for a fresh conversion.
Queues, jobs, commands, and notifications
There is no request, session, or authenticated user in a queued job, an
Artisan command, or a queued notification/mail. In those contexts the cast falls
back to config('smart-timezone.default'). When you need a specific user's zone
outside a request, convert explicitly:
Configuration
Published to config/smart-timezone.php:
Opting out of automatic middleware
By default the package pushes DetectTimezone onto the web group and
SetTimezoneFromHeader onto the api group. Set register_middleware to
false and add them where you want:
Custom route prefix + JS
If you change route_prefix, tell the detector script where to POST:
Events
ImranAli\SmartTimezone\Events\TimezoneDetected fires whenever a timezone is
resolved and written to the session (web middleware, API header middleware, or
the JS route). It carries $timezone, $source
(header / cookie / user / ip / route / default), and $request.
A common use — copy a guest's detected zone onto their account at registration:
Commands
Requirements
- PHP 8.1+
- Laravel 10, 11, 12, or 13
torann/geoip— optional, only needed if you keep'ip'indetect_from. Install it yourself withcomposer require torann/geoip. Without it, IP detection is skipped silently.
Package Comparison
| Package | Auto JS Detect | API Header | Eloquent Cast | DB Saves UTC | SPA Ready |
|---|---|---|---|---|---|
| SmartTimezone | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
jamesmills/laravel-timezone |
❌ No | ❌ No | ✅ Yes | ✅ Yes | ❌ No |
spatie/laravel-timezone |
⚠️ Manual | ❌ No | ❌ No | ❌ No | ❌ No |
Testing
Contributing
PRs welcome! Please ensure tests pass before submitting:
Security
- All timezone input (header, cookie, request body) is validated against
timezone_identifiers_list()before use. - The JS detection route (
POST {route_prefix}/set) runs behind thewebmiddleware (CSRF) and athrottlelimiter (route_throttle). - The detection cookie is
SameSite=LaxandSecureon HTTPS. It is notHttpOnlyby design — the browser script needs to read it. - IP detection sends the visitor's IP to whatever
torann/geoipdriver you configure. Drop'ip'fromdetect_fromif that is a concern.
If you discover a security vulnerability, please email [email protected].
License
MIT. Use it in any project, commercial or personal.
Built by Imran Ali in Lahore, PK 🇵🇰
If this saved you from timezone hell, ⭐ star the repo!
All versions of smart-timezone with dependencies
illuminate/console Version ^10.0|^11.0|^12.0|^13.0
illuminate/contracts Version ^10.0|^11.0|^12.0|^13.0
illuminate/database Version ^10.0|^11.0|^12.0|^13.0
illuminate/http Version ^10.0|^11.0|^12.0|^13.0
illuminate/routing Version ^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^10.0|^11.0|^12.0|^13.0
nesbot/carbon Version ^2.67|^3.0