datetime casts not considering timezone
ithuis opened this issue · comments
- Laravel-mongodb Version: 4.1
- PHP Version: 8.2
- Database Driver & Version:
Description:
When using a timezone like "Europe\Amsterdam" the date is stored in UTC but when retrieved is not transformed in local time.
Steps to reproduce
- create a model
- dd( $model->created_at , Carbon::now() )
Expected behaviour
same ( not exactly ) date and time
Actual behaviour
created_at is 1 hour earlier
The timezone is never stored in the database. Neither with Eloquent, nor with the MongoDB overlay. But there is indeed a difference in treatment when the value is read.
With Eloquent, it's read with the current timezone of PHP, whereas with MongoDB, we systematically return it in UTC. This is because MongoDB explicitly states that only UTC dates can be stored.
MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.