Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ Pull in the package using Composer
composer require jamesmills/laravel-timezone
```

Add TimezoneModelTrait into your resource Model

```
<?php

namespace App;

class Foo
{
use TimezoneModelTrait;
}
```

## Database

By default, this package is shipped with database migration,
which creates a new column `timezone` into the `users` table.

### Using Default Migrations
Publish database migrations

```
Expand All @@ -52,6 +71,11 @@ Run the database migrations. This will add a `timezone` column to your `users` t
php artisan migrate
```

### Custom Data Source

If you use a different data source for your timezone,
you can just override both methods in the `TimezoneModelTrait` on your resource model to your needs.

## Examples

### Showing date/time to the user in their timezone
Expand Down
15 changes: 7 additions & 8 deletions src/Listeners/Auth/UpdateUsersTimezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class UpdateUsersTimezone
{

/**
* Handle the event.
*
Expand Down Expand Up @@ -50,9 +49,9 @@ public function handle($event)
$ip = $this->getFromLookup();
$geoip_info = geoip()->getLocation($ip);

if ($user->timezone != $geoip_info['timezone']) {
if (config('timezone.overwrite') == true || $user->timezone == null) {
$user->timezone = $geoip_info['timezone'];
if ($user->getTimezone() != $geoip_info['timezone']) {
if (config('timezone.overwrite') == true || $user->getTimezone() == null) {
$user->setTimezone($geoip_info['timezone']);
$user->save();

$this->notify($geoip_info);
Expand All @@ -69,7 +68,7 @@ private function notify(Location $geoip_info)
return;
}

$message = 'We have set your timezone to ' . $geoip_info['timezone'];
$message = 'We have set your timezone to '.$geoip_info['timezone'];

if (config('timezone.flash') == 'laravel') {
request()->session()->flash('success', $message);
Expand Down Expand Up @@ -103,8 +102,8 @@ private function notify(Location $geoip_info)
}

/**
* @return mixed
*/
* @return string|null
*/
private function getFromLookup()
{
$result = null;
Expand Down Expand Up @@ -134,7 +133,7 @@ private function lookup($type, $keys)
$value = null;

foreach ($keys as $key) {
if (!request()->$type->has($key)) {
if (! request()->$type->has($key)) {
continue;
}
$value = request()->$type->get($key);
Expand Down
14 changes: 7 additions & 7 deletions src/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Timezone
* @param bool $format_timezone
* @return string
*/
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string
{
if (is_null($date)) {
return 'Empty';
}

$timezone = (auth()->user()->timezone) ?? config('app.timezone');
$timezone = (auth()->user()->getTimezone()) ?? config('app.timezone');

$date->setTimezone($timezone);

Expand All @@ -29,7 +29,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
$formatted_date_time = $date->format($format);

if ($format_timezone) {
return $formatted_date_time . ' ' . $this->formatTimezone($date);
return $formatted_date_time.' '.$this->formatTimezone($date);
}

return $formatted_date_time;
Expand All @@ -39,22 +39,22 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
* @param $date
* @return Carbon
*/
public function convertFromLocal($date) : Carbon
public function convertFromLocal($date): Carbon
{
return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC');
return Carbon::parse($date, auth()->user()->getTimezone())->setTimezone('UTC');
}

/**
* @param Carbon $date
* @return string
*/
private function formatTimezone(Carbon $date) : string
private function formatTimezone(Carbon $date): string
{
$timezone = $date->format('e');
$parts = explode('/', $timezone);

if (count($parts) > 1) {
return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0];
return str_replace('_', ' ', $parts[1]).', '.$parts[0];
}

return str_replace('_', ' ', $parts[0]);
Expand Down
25 changes: 25 additions & 0 deletions src/Traits/TimezoneModelTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace JamesMills\LaravelTimezone\Traits;

trait TimezoneModelTrait
{
/**
* @return mixed
*/
public function getTimezone()
{
return $this->timezone;
}

/**
* @param string $timezone
* @return $this
*/
public function setTimezone(string $timezone)
{
$this->timezone = $timezone;

return $this;
}
}