Today I used the MySQL convert_tz function.
Before I could use named timezones with convert_tz I had to load the timezones.
Today I used the MySQL convert_tz function.
Before I could use named timezones with convert_tz I had to load the timezones.
Turns out MySQL doesn’t load timezone info by default! As you can read about here.
To load MySQL timezone info:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Today I had to use PHP’s DateTimeZone->getOffset() to get a timezone offset. It needed formatting which I did with:
function timezone_offset() { $offset = timezone()->getOffset( now() ); $hours = round( abs( $offset ) / 3600 ); $minutes = round( ( abs( $offset ) - $hours * 3600 ) / 60 ); $result = ( $offset < 0 ? '-' : '+' ) . ( $hours < 10 ? '0' : '' ) . $hours . ':' . ( $minutes < 10 ? '0' : '' ) . $minutes; return $result; }