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 two types of date-time types:
Instant
type.
The following table presents a matrix of available DB date-time types:
Local DateTime no timezone handling |
DateTime timezone conversion |
DateTime timezone stored |
|
---|---|---|---|
MySQL | datetime |
timestamp |
- |
Postgres | timestamp |
timestamptz |
- |
SQL Server | datetime , datetime2 |
- | datetimeoffset |
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.
Dbal comes with two query modifiers:
Type | Modifier | Description |
---|---|---|
local datetime | %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; |
Type | Modifier | Comment |
---|---|---|
local datetime | %ldt |
timezone (offset) is removed |
datetime | %dt |
value is converted to connection timezone and timezone offset is removed if properly stored to timestamp column type |
Type | Column Type | Comment |
---|---|---|
local datetime | datetime |
value is converted into application timezone |
datetime | timestamp |
value is interpreted in connection timezone and converted into application timezone |
By default, MySQL server does not support named timezones, see the setup chapter 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
.
Type | Modifier | Comment |
---|---|---|
local datetime | %ldt |
timezone (offset) is removed |
datetime | %dt |
value is converted to connection timezone and timezone offset is removed if properly stored to timestamptz column type |
Type | Column Type | Comment |
---|---|---|
local datetime | timestamp |
value is converted into application timezone |
datetime | timestamptz |
value is converted into application timezone |
Type | Modifier | Comment |
---|---|---|
local datetime | %ldt |
timezone (offset) is removed |
datetime | %dt |
no timezone conversion is done and the timezone offset is stored in datetimeoffset db type |
Type | Column Type | Comment |
---|---|---|
local datetime | datetime |
value is converted into application timezone |
datetime | datetimeoffset |
value is read with timezone offset and no further modification is done - i.e. no application timezone conversion happens |