Download the PHP package petrknap/zoned-datetime-persistence without Composer
On this page you can find all versions of the php package petrknap/zoned-datetime-persistence. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download petrknap/zoned-datetime-persistence
More information about petrknap/zoned-datetime-persistence
Files in petrknap/zoned-datetime-persistence
Package zoned-datetime-persistence
Short Description Timezone aware date-time persistence
License LGPL-3.0-or-later
Homepage https://github.com/petrknap/zoned-datetime-persistence
Informations about the package zoned-datetime-persistence
Timezone aware date-time persistence
Many data storage systems (like MySQL) do not natively support storing timezone information alongside date-time values. This limitation introduces ambiguity when handling zoned date-times — particularly in applications operating across multiple timezones or even within a single timezone that observes multiple offsets (e.g. due to daylight saving time).
This package addresses the issue by providing tools that treat zoned date-time as a pair consisting of:
- the UTC date-time value, and
- a companion value that explicitly captures the corresponding timezone information.
Implemented
- UTC with local date-time
- How to use it
- UTC with timezone
- UTC with system timezone
- UTC date-time converter / type / cast
UTC with local date-time
UtcWithLocal
The most useful approach is to store the UTC date-time together with its local counterpart. This dual representation enables seamless manipulation of date-time values directly within storage system. The local date-time is ideal for grouping and filtering based on user or business context, while the UTC value ensures consistent and accurate sorting across timezones.
How to use it
There is built-in support for
the Jakarta Persistence API (see JpaTest.java),
the Doctrine ORM (see DoctrineTest.php),
the Eloquent (see EloquentTest.php),
and, of course, it can be integrated manually into any project, giving you full flexibility to adapt it to your specific needs.
UTC with timezone
UtcWithTimezone
If you want to preserve the original timezone as is, you cannot use UtcWithLocal, because it works over fixed offsets.
In this case, you need to use this implementation.
UTC with system timezone
UtcWithSystemTimezone
The most compact approach is to store only the UTC date-time.
This serves as an alternative to MySQL's TIMESTAMP, Postgres's TIMESTAMP WITH TIMEZONE, and custom ORM types.
It offers full range of DateTime, avoids normalization on connection, adds .utc into your queries for better readability and didn't need special configuration.
UTC date-time converter / type / cast
UtcDateTimeConverterJakarta Persistence API
This converter transparently manages conversions of ZonedDateTime, including JPQL parameters.
That means you no longer need to worry about manual timezone adjustments.
For examples, see the JpaTest.
UtcDateTimeTypeDoctrine ORM
In contrast to UtcDateTimeConverter, this type does not automatically adjust the timezone of DQL parameters.
You must therefore provide the type when you are calling setParameter on your queries.
Also, you have to register the type in your Doctrine configuration manually.
For examples, see the DoctrineTest.
AsUtcDateTimeEloquent
In contrast to UtcDateTimeConverter and UtcDateTimeType, this cast may or may not adjust the timezone of any input.
You should therefore handle timezone conversions explicitly everytime you are providing date-time into Eloquent.
But the conversion after hydration works well.
For examples, see the EloquentTest.
You can support this project via donation.
The project is licensed under the terms of the LGPL-3.0-or-later.