Time Zone Configuration

This plugin allows to specify a time zone and to format time/date display

'; if( defined('YOURLS_HOURS_OFFSET') ) { print '

Note: you have YOURLS_HOURS_OFFSET defined in your config.php. This plugin will override this setting.

'; } print '
'; // Time zones drop down print '

Time zone:

Choose a city near your location, in the same timezone as you, or a UTC time offset.

'; yourls_tzp_tz_dropdown( $user_time_zone ); print '

Universal time (UTC) time is: ' . date( 'Y-m-d H:i:s', yourls_tzp_timezoned_timestamp( time(), 'UTC' ) ). '

'; if($user_time_zone) { print "

Time in $user_time_zone_display is: " . date( 'Y-m-d H:i:s', yourls_tzp_timezoned_timestamp( time(), $user_time_zone) ) . '

'; } print '
'; // Display radio button for date format $choices = array( 'j F Y', // 13 April 2020 'F j, Y', // May 10, 2020 'd/m/Y', // 20/10/2020 'm/d/Y', // 10/20/2020 'Y/m/d', // 2020/10/20 ); yourls_tzp_format_radio( 'Date Format', 'date_format', $choices, $user_time_zone, $user_date_format, $user_date_format_custom ); // Display radio button for date format $choices = array( 'H:i', // 21:23 'g:i a', // 9:23 pm 'g:i A', // 9:23 PM ); yourls_tzp_format_radio( 'Time Format', 'time_format', $choices, $user_time_zone, $user_time_format, $user_time_format_custom ); print '

Note: custom formats need a PHP date() string.'; } /** * Output CSS & JS */ function yourls_tzp_js_css() { $plugin_url = yourls_plugin_url( __DIR__ ); print << JSCSS; } /** * Draw the time zone drop down * * @param string $user_time_zone Timezone to be marked as "selected" in the dropdown */ function yourls_tzp_tz_dropdown( $user_time_zone ) { // Continent list $continent = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC, ); // Timezones per continents $timezones = array(); foreach ($continent as $name => $mask) { $zones = DateTimeZone::listIdentifiers($mask); foreach($zones as $timezone) { // Remove region name $timezones[$name][$timezone] = substr($timezone, strlen($name) +1); } } // Manual UTC offset $offset_range = array( '-12', '-11:30', '-11', '-10:30', '-10', '-9.5', '-9', '-8:30', '-8', '-7:30', '-7', '-6:30', '-6', '-5:30', '-5', '-4:30', '-4', '-3:30', '-3', '-2:30', '-2', '-1:30', '-1', '-0:30', 'UTC', '+0:30', '+1', '+1:30', '+2', '+2:30', '+3', '+3:30', '+4', '+4:30', '+5', '+5:30', '+5:45', '+6', '+6:30', '+7', '+7:30', '+8', '+8:30', '+8:45', '+9', '+9:30', '+10', '+10:30', '+11', '+11:30', '+12', '+12:45', '+13', '+13:45', '+14' ); foreach( $offset_range as $offset_name ) { $offset_value = $offset_name; $offset_name = str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), $offset_name ); if ($offset_name != 'UTC') { $offset_name = 'UTC' . $offset_name; } // $offset_value = 'UTC' . $offset_value; $timezones['UTC'][$offset_value] = $offset_name; } print ''; } /** * Output radio button list * * @param string $title Dropdown title * @param string $input_name Dropdown 'radio' name * @param array $formats List of available choices, to which 'custom' will be appended * @param string $tz Time zone * @param string $selected Checked radio value * @param string $custom Custom format value */ function yourls_tzp_format_radio( $title, $input_name, $formats, $tz, $selected, $custom ) { print "

$title:

"; foreach ($formats as $format) { $checked = ( $format === $selected ) ? 'checked="checked"' : '' ; print "

\n"; } $checked = ( 'custom' === $selected ) ? 'checked="checked"' : '' ; $preview = date( $custom, yourls_tzp_timezoned_timestamp( time(), $tz ) ); print "\n"; print '
'; } /** * Update time zone in database */ function yourls_tzp_config_update_settings() { $settings = array( 'time_zone' => yourls_tzp_get_value($_POST, 'time_zone', 'UTC'), 'date_format' => yourls_tzp_get_value($_POST, 'date_format', 'Y/m/d'), 'date_format_custom' => yourls_tzp_get_value($_POST, 'date_format_custom_value', 'Y/m/d'), 'time_format' => yourls_tzp_get_value($_POST, 'time_format', 'H:i'), 'time_format_custom' => yourls_tzp_get_value($_POST, 'time_format_custom_value', 'H:i'), ); yourls_update_option( 'timezone', $settings ); return $settings; }