Last active
September 28, 2021 14:05
-
-
Save Illizian/4d4b66129cecba4d6ace9b0b6ab0e1da to your computer and use it in GitHub Desktop.
Laravel localised DateTimes with UTC Database storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@props(['date', 'relative', 'classes']) | |
<time datetime="{{ $date->toIso8601String() }}"> | |
@unless($relative) | |
{{ $date }} | |
@else | |
{{ $date->diffForHumans(now()) }} | |
@endunless | |
</time> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\View\Components; | |
use Illuminate\Contracts\View\View; | |
use Illuminate\Support\Carbon; | |
use Illuminate\View\Component; | |
class DateTime extends Component | |
{ | |
public function __construct( | |
public Carbon $date, | |
public bool $relative = false, | |
public string $classes = '', | |
) { | |
// Use either the User's preferred timezone or the app configured one | |
$timezone = auth()->user()->timezone ?? config('app.timezone'); | |
// Extract locale from request header or the app configured one | |
[ $locale ] = explode( | |
',', | |
request()->header('Accept-Language', config('app.locale')) | |
); | |
// Apply User's prefences | |
$this->date | |
->timezone($timezone) | |
->locale($locale); | |
} | |
/** | |
* Get the view / contents that represent the component. | |
* | |
* @return \Illuminate\Contracts\View\View|\Closure|string | |
*/ | |
public function render(): View | |
{ | |
return view('components.date-time'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\Traits; | |
use Carbon\Carbon; | |
trait DateTime | |
{ | |
/** | |
* If Carbon - Set the timezone to `app.timezone` configuration | |
* | |
* @param mixed $value | |
* | |
* @return \Carbon\Carbon|mixed | |
*/ | |
protected function asDateTime($value) | |
{ | |
if($value instanceof Carbon) { | |
$value->timezone(config('app.timezone')); | |
} | |
return parent::asDateTime($value); | |
} | |
/** | |
* If Carbon - Set the timezone to `app.timezone` configuration | |
* | |
* @param mixed $value | |
* | |
* @return \Carbon\Carbon|mixed | |
*/ | |
public function fromDateTime($value) | |
{ | |
if($value instanceof Carbon) { | |
$value->timezone(config('app.timezone')); | |
} | |
return parent::fromDateTime($value); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use App\Models\Traits\DateTime; | |
use Illuminate\Database\Eloquent\Model; | |
// [...] | |
class ExampleModel extends Model | |
{ | |
use DateTime; | |
// [...] | |
/** | |
* The attributes that should be cast. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'a_date' => 'datetime', | |
'anoter_date' => 'datetime', | |
]; | |
// [...] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<x-date-time :date="App\Models\ExampleModel::find(1)->a_date" /> | |
<x-date-time :relative="true" :date="App\Models\ExampleModel::find(1)->a_date" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cannot include
/
in filenames on Gist, so updated Class names to indicate type, and you can use namespaces to assign directory structure