Skip to content

Instantly share code, notes, and snippets.

@ediamin
Created December 7, 2016 08:53
Show Gist options
  • Save ediamin/7121c82c5f1cad3bd0aee02392518bcf to your computer and use it in GitHub Desktop.
Save ediamin/7121c82c5f1cad3bd0aee02392518bcf to your computer and use it in GitHub Desktop.
WordPress - Moment Timezone times
// required moment-timezone-with-data.js
String.prototype.toLocalTime = function() {
return moment(moment.tz(this, 'YYYY-MM-DD HH:mm:ss', wpTimezone))
.tz(moment.tz.guess())
.format('YYYY-MM-DD HH:mm:ss');
};
String.prototype.toWPTime = function() {
return moment(this, 'YYYY-MM-DD HH:mm:ss').tz(wpTimezone).format('YYYY-MM-DD HH:mm:ss');
};
<?php
/**
* WP Timezone Settings
*
* @return string
*/
public static function get_wp_timezone() {
$momentjs_tz_map = [
'UTC-12' => 'Etc/GMT+12',
'UTC-11.5' => 'Pacific/Niue',
'UTC-11' => 'Pacific/Pago_Pago',
'UTC-10.5' => 'Pacific/Honolulu',
'UTC-10' => 'Pacific/Honolulu',
'UTC-9.5' => 'Pacific/Marquesas',
'UTC-9' => 'America/Anchorage',
'UTC-8.5' => 'Pacific/Pitcairn',
'UTC-8' => 'America/Los_Angeles',
'UTC-7.5' => 'America/Edmonton',
'UTC-7' => 'America/Denver',
'UTC-6.5' => 'Pacific/Easter',
'UTC-6' => 'America/Chicago',
'UTC-5.5' => 'America/Havana',
'UTC-5' => 'America/New_York',
'UTC-4.5' => 'America/Halifax',
'UTC-4' => 'America/Manaus',
'UTC-3.5' => 'America/St_Johns',
'UTC-3' => 'America/Sao_Paulo',
'UTC-2.5' => 'Atlantic/South_Georgia',
'UTC-2' => 'Atlantic/South_Georgia',
'UTC-1.5' => 'Atlantic/Cape_Verde',
'UTC-1' => 'Atlantic/Azores',
'UTC-0.5' => 'Atlantic/Reykjavik',
'UTC+0' => 'Etc/UTC',
'UTC' => 'Etc/UTC',
'UTC+0.5' => 'Etc/UTC',
'UTC+1' => 'Europe/Madrid',
'UTC+1.5' => 'Europe/Belgrade',
'UTC+2' => 'Africa/Tripoli',
'UTC+2.5' => 'Asia/Amman',
'UTC+3' => 'Europe/Moscow',
'UTC+3.5' => 'Asia/Tehran',
'UTC+4' => 'Europe/Samara',
'UTC+4.5' => 'Asia/Kabul',
'UTC+5' => 'Asia/Karachi',
'UTC+5.5' => 'Asia/Kolkata',
'UTC+5.75' => 'Asia/Kathmandu',
'UTC+6' => 'Asia/Dhaka',
'UTC+6.5' => 'Asia/Rangoon',
'UTC+7' => 'Asia/Bangkok',
'UTC+7.5' => 'Asia/Bangkok',
'UTC+8' => 'Asia/Shanghai',
'UTC+8.5' => 'Asia/Pyongyang',
'UTC+8.75' => 'Australia/Eucla',
'UTC+9' => 'Asia/Tokyo',
'UTC+9.5' => 'Australia/Darwin',
'UTC+10' => 'Australia/Brisbane',
'UTC+10.5' => 'Australia/Adelaide',
'UTC+11' => 'Australia/Melbourne',
'UTC+11.5' => 'Pacific/Norfolk',
'UTC+12' => 'Asia/Anadyr',
'UTC+12.75' => 'Asia/Anadyr',
'UTC+13' => 'Pacific/Fiji',
'UTC+13.75' => 'Pacific/Chatham',
'UTC+14' => 'Pacific/Tongatapu',
];
$current_offset = get_option('gmt_offset');
$tzstring = get_option('timezone_string');
// Remove old Etc mappings. Fallback to gmt_offset.
if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) {
$tzstring = '';
}
if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
if ( 0 == $current_offset ) {
$tzstring = 'UTC+0';
} elseif ($current_offset < 0) {
$tzstring = 'UTC' . $current_offset;
} else {
$tzstring = 'UTC+' . $current_offset;
}
}
if ( array_key_exists( $tzstring , $momentjs_tz_map ) ) {
$tzstring = $momentjs_tz_map[ $tzstring ];
}
return $tzstring;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment