PHP code example of syriable / laravel-user-context
1. Go to this page and download the library: Download syriable/laravel-user-context 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/ */
syriable / laravel-user-context example snippets
$user->isOnline(); // true
$user->presence()->lastSeen(); // CarbonImmutable|null
$user->location()->countryName(); // "Sweden" (from ISO country code)
$user->timezone()->now(); // CarbonImmutable in the user's zone
$user->greeting(); // "Good afternoon"
// A user in New York checking the best time to message a user in Shanghai:
$comparison = $newYorkUser->timeFor($shanghaiUser);
$comparison->formattedOffset(); // "+12:00"
$comparison->isNight(); // true → maybe wait
$comparison->isConvenientTime(); // false → not a good time to ping
use Syriable\UserContext\Concerns\HasUserContext;
class User extends Authenticatable
{
use HasUserContext;
}
$user->timezone()->name(); // "Asia/Shanghai" (or null if unknown)
$user->localTime(); // CarbonImmutable in their timezone
$user->isNight(); // bool
$user->greeting(); // localized "Good evening"
$user->locale(); // "en_US"
// Explicit overrides always win over IP / header detection:
UserContext::overrideTimezone($user, 'Europe/Berlin');
UserContext::overrideLocale($user, 'de');
$user->location()->countryCode(); // "SE"
$user->location()->countryName(); // "Sweden"
$user->location()->city(); // "Stockholm"
$user->location()->ipAddress(); // last known IP (raw in sessions mode)
$user->location()->userAgent(); // last known User-Agent, or null
$c = $userA->timeFor($userB);
$c->theirTime; // CarbonImmutable in B's timezone
$c->formattedOffset(); // "+12:00"
$c->dayPeriod; // DayPeriod::Night
$c->isConvenientTime(); // is it a reasonable local hour to contact B?