codewiser / laravel-cast-datetime-tz
Cast datetime with timezone for Laravel
v1.0.3
2024-04-26 11:51 UTC
Requires
- php: ^8.0.2
- laravel/framework: ^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^10.1
README
Laravel doesn't respect timezone. Cast \Codewiser\Casts\AsDatetimeWithTZ
fixes this wrong behaviour.
Before
class Article extends \Illuminate\Database\Eloquent\Model { protected $casts = [ 'date' => 'datetime' ]; }
// e.g. Laravel has Europe/London (+01:00) timezone config()->set('app.timezone', 'Europe/London'); $model = new Article(); $model->date = '2000-01-01T10:00:00+02:00'; echo $model->date->format('c'); // Expecting 2000-01-01T09:00:00+01:00 // Actual 2000-01-01T10:00:00+01:00
After
class Article extends \Illuminate\Database\Eloquent\Model { protected $casts = [ 'date' => \Codewiser\Casts\AsDatetimeWithTZ::class ]; }
// e.g. Laravel has Europe/London (+01:00) timezone config()->set('app.timezone', 'Europe/London'); $model = new Article(); $model->date = '2000-01-01T10:00:00+02:00'; echo $model->date->format('c'); // Expecting 2000-01-01T09:00:00+01:00 // Actual 2000-01-01T09:00:00+01:00