Use CONVERT_TZ() or SET time_zone to shift DATETIME/TIMESTAMP values between zones in MariaDB.
CONVERT_TZ(datetime_expr, from_tz, to_tz) shifts a DATETIME or TIMESTAMP from one zone or offset to another. It returns NULL if either zone is unknown or missing from the time-zone tables.
Run mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
. Without this data, named zones like 'America/New_York' are unresolved and CONVERT_TZ() fails.
SET time_zone = 'UTC';
changes the conversion context for NOW(), CURRENT_TIMESTAMP, and TIMESTAMP arithmetic for the current connection only.
Store all timestamps in UTC for consistency and index efficiency. Convert to the user’s zone at query time with CONVERT_TZ() or by switching @@session.time_zone.
SELECT o.id,
o.order_date AS utc_time,
CONVERT_TZ(o.order_date, '+00:00', c_tz.tz) AS local_time
FROM Orders o
JOIN Customers c ON c.id = o.customer_id
JOIN CustomerTimezones c_tz ON c_tz.customer_id = c.id;
Pass offsets directly: CONVERT_TZ(order_date, '+00:00', '-05:00')
. Useful when the client sends only the offset.
If CONVERT_TZ() returns NULL, the zone is missing. Filter with WHERE local_time IS NULL
and alert or fallback to UTC.
• Keep DATETIME/TIMESTAMP columns in UTC.
• Store each user’s IANA zone or numeric offset in a profile table.
• Use CONVERT_TZ() in SELECT or in a VIEW, not during INSERT.
• Index UTC columns for date-range searches to stay sargable.
No. DATE has no time component. Cast to DATETIME or append a time portion first.
Yes, add default_time_zone='+00:00'
in my.cnf or run SET GLOBAL time_zone = '+00:00'
, but ensure applications expect UTC.
Yes, when you use IANA zone names and the time-zone tables are loaded. Numeric offsets do not adjust automatically for DST.