-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
89 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,84 @@ | ||
DateTime TimeZones Support | ||
########################## | ||
DateTime | ||
######## | ||
|
||
Database engines provide different types for storing date-times. Also, the type naming is often misleading. This documentation page covers the basics and Dbal's solution to the datetime & timezone problem. | ||
Database engines provide different types for storing date-times. Also, the type naming is often misleading. This documentation chapter covers the basics and Dbal's solution to the datetime & timezone handling. | ||
|
||
Generally, we recognize three types of date time types: | ||
Generally, we recognize two types of date-time types: | ||
|
||
- **Local DateTime** - it is a date time which has not an exact position on the *time-line*; simply, we do not know in which time zone the event happened, therefore we consider the information as a local; example: date time when the school year begins, since the country may be across more the timezones, this type of information may be stored as a local date time, i.e. striping the "exactness" may be an advantage here; | ||
- **UTC DateTime** - is an exact timestamp on the *time-line*; example: timestamp of the meeting start in a calendar; | ||
- **Zoned DateTime** - is an exact timestamp on the *time-line* plus an added context of specific time-zone; either we use reader's timezone or timezone of the location when the timestamp "happened"; example: presenting an online streaming event start - since it is pretty usual that this event will be watched from multiple times, we need add reader's timezone context to the *stored* UTC DateTime. | ||
- **DateTime** - is an exact timestamp on the *time-line*; example: timestamp of the meeting start in a calendar; this type is also referred as an `Instant` type. | ||
- **UTC DateTime** - having this type represented in UTC means we don't know an exact context where it happened; it could be in the day or in the night; | ||
- **Zoned DateTime** - is an exact timestamp on the *time-line* plus an additional context of specific time-zone; either we use reader's timezone or timezone of the location where the timestamp "happened"; example: presenting an online streaming event start - since it is pretty usual that this event will be watched from multiple places, we need add reader's timezone context to the *stored* UTC DateTime. | ||
|
||
The following table presents a matrix of available data-time types and their behavior: | ||
The following table presents a matrix of available DB date-time types: | ||
|
||
|------------------------------------------------------------------------------ | ||
| | Local DateTime | UTC DateTime | Zoned DateTime | ||
| | Local DateTime | DateTime || | ||
| | no timezone handling | timezone conversion | timezone stored | ||
|------------------------------------------------------------------------------ | ||
| MySQL | `datetime` | `timestamp` | - | ||
| Postgres | `timestamp` | `timestamptz` | - | ||
| SQL Server | `datetime`, `datetime2` | - | `datetimeoffset` | ||
|
||
- **no timezone handling**: this is straightforward attitude; database just store your time-stamp and does not do any modification to the timestamp; this is the easiest solution, but brings a disadvantage: database cannot exactly diff two timestamps, i.e. it maz produce wrong results because day-light saving shift` | ||
- **timezone conversion**: database stores the time stamp unified in UTC and for MySQL, it converts the timestamp to connection's timezone for every read and write; | ||
- **no timezone handling**: database stores the time-stamp and does not do any modification to it; this is the easiest solution, but brings a disadvantage: database cannot exactly diff two time-stamps, i.e. it may produce wrong results because day-light saving shift is needed but db does not know which zone to use for the calculation; | ||
- **timezone conversion**: database stores the time-stamp unified in UTC and reads it in connection's timezone; | ||
- **timezone stored**: database does not do any conversion, it just stores the timezoned timestamp and returns it back; | ||
|
||
Dbal offers **connection time zone** configuration option (`connectionTz`) which defines the timezone for database connection communication; by default it equals to PHP's current default timezone. This option is configured by timezone name, e.g. `Europe/Prague` string. | ||
Dbal offers a **connection time zone** configuration option (`connectionTz`) that defines the timezone for database connection communication; it equals to PHP's current default timezone by default. This option is configured by a timezone name, e.g. `Europe/Prague` string. | ||
|
||
/--div .[note] | ||
By default, MySQL server doesn't support named timezones, see [the setup chapter | timezones-mysql-support] how to configure them. Still, there is a possibility to pass only a timezone offset, e.g. `+03:00`, but this is not ideal. Use magic `auto-offset` value that will be dynamically converted to the current PHP's timezone offset. | ||
Dbal comes with two query modifiers: | ||
|
||
This will make Dbal fully functional, although some SQL queries and expressions may not return correctly calculated results, e.g. functions that calculate two-date operations directly in the database - `TIMEDIFF`, `ADDDATE`, etc. | ||
\-- | ||
|* localdatetime | `%ldt` | passes DateTime(Interface) object as it is, without any timezone conversion and identification; formerly known as datetime simple (`%dts`) | ||
|* datetime | `%dt` | converts DateTime(Interface) object to connection timezone; | ||
|
||
Storing | ||
======= | ||
-------------------------- | ||
|
||
Dbal comes with two query modifiers: | ||
- **`%dt`** (as datetime): converts DateTime object to connection timezone; | ||
- **`%ldt`** (as local datetime): pass DateTime object as it is, without any timezone conversion and identification; formerly known as datetime simple (%dts); | ||
MySQL | ||
***** | ||
|
||
|------------------------------------------------------------------------------ | ||
| | Local DateTime | UTC DateTime | Zoned DateTime | ||
| | no timezone handling | timezone conversion | timezone stored | ||
|------------------------------------------------------------------------------ | ||
| MySQL | `%ldt` | `%dt` | - | ||
| Postgres | `%ldt` | `%dt` | - | ||
| SQL Server | `%ldt` | - | `%dt` | ||
.[fixed] | ||
|* Writing || | ||
|* local datetime |* datetime | ||
| `%ldt` modifier | `%dt` modifier | ||
| timezone (offset) is removed | value is converted to connection timezone and timezone offset is removed if properly stored to `timestamp` column type | ||
|* Reading || | ||
|* local datetime |* datetime | ||
| `datetime` column type | `timestamp` column type | ||
| value is converted into application timezone | value is interpreted in connection timezone and converted into application timezone | ||
|
||
Connection Time Zone | ||
-------------------- | ||
|
||
By default, MySQL server does not support named timezones, see [the setup chapter | timezones-mysql-support] how to configure them. Still, there is a possibility to pass only a timezone offset configuraion, e.g. `+03:00`, but this is not ideal. Use rather magic `auto-offset` value that will be dynamically converted to the current PHP's timezone offset. | ||
|
||
This will make Dbal fully functional, although some SQL queries and expressions may not return correctly calculated results, e.g. functions calculating two-date operations directly in the database - `TIMEDIFF`, `ADDDATE`. | ||
|
||
-------------------------- | ||
|
||
Postgres | ||
******** | ||
|
||
.[fixed] | ||
|* Writing || | ||
|* local datetime |* datetime | ||
| `%ldt` modifier | `%dt` modifier | ||
| timezone (offset) is removed | value is converted to connection timezone and timezone offset is removed if properly stored to `timestamptz` column type. | ||
|* Reading || | ||
|* local datetime |* datetime | ||
| `timestamp` column type | `timestamptz` column type | ||
| value is converted into application timezone | value is converted into application timezone | ||
|
||
-------------------------- | ||
|
||
Reading | ||
======= | ||
SQL Server | ||
********** | ||
|
||
Database drivers also correctly read the stored values and convert them into `DateTimeImmutable` instances: | ||
- **MySQL** driver interprets `timestamp` column in connection's timezone and converts it to PHP's default timezone; | ||
- **Postgres** driver reads `timestamptz` (timezoned timestamp) and converts it to PHP's default timezone; | ||
- **SQL server** driver reads `datetimeoffset` (timezoned timestamp) and does not do any timezone conversion; | ||
.[fixed] | ||
|* Writing || | ||
|* local datetime |* datetime | ||
| `%ldt` modifier | `%dt` modifier | ||
| timezone (offset) is removed | no timezone conversion is done and the timezone offset is stored in `datetimeoffset` db type | ||
|* Reading || | ||
|* local datetime |* datetime | ||
| `datetime` or `datetime2` column type | `datetimeoffset` column type | ||
| value is converted into application timezone | value is read with timezone offset and no further modification is done - i.e. no application timezone conversion happens |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.