diff --git a/README.md b/README.md index 03b9416..59ebc53 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,31 @@ And with custom formatting // 2018-07-04 3:32 New York, America ``` +### Using models casting class + +#### Basic usage + +``` + Timezone::class, + ]; +} +``` + ### Saving the users input to the database in UTC This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance. diff --git a/src/Casts/Timezone.php b/src/Casts/Timezone.php new file mode 100644 index 0000000..2a145f0 --- /dev/null +++ b/src/Casts/Timezone.php @@ -0,0 +1,45 @@ +setTimezone($this->getUserTimezone()); + } + + /** + * Transform the attribute to its underlying model values. + * + * @param Model $model + * @param string $key + * @param mixed $value + * @param array $attributes + * @return Carbon + */ + public function set($model, string $key, $value, array $attributes) + { + return TimezoneFacade::convertFromLocal($value); + } +} diff --git a/src/Timezone.php b/src/Timezone.php index 7e9c9d9..ff69cb7 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -3,24 +3,25 @@ namespace JamesMills\LaravelTimezone; use Carbon\Carbon; +use JamesMills\LaravelTimezone\Traits\TimezoneTrait; class Timezone { + use TimezoneTrait; + /** * @param Carbon|null $date * @param null $format * @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'); - - $date->setTimezone($timezone); + $date->setTimezone($this->getUserTimezone()); if (is_null($format)) { return $date->format(config('timezone.format')); @@ -29,7 +30,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; @@ -39,22 +40,23 @@ 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()->timezone) + ->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]); diff --git a/src/Traits/TimezoneTrait.php b/src/Traits/TimezoneTrait.php new file mode 100644 index 0000000..75f77d5 --- /dev/null +++ b/src/Traits/TimezoneTrait.php @@ -0,0 +1,14 @@ +user()->timezone) ?? config('app.timezone'); + } +}