public static function GetTimeZone($timezone) { try { return timezone_name_get(new DateTimeZone($timezone)); } catch (Exception $ex) { return $ex->getMessage(); } }
/** * Return a set of timezone information relevant to this joyful stuff :D */ public static function zoneInfo() { $zones = array(); $now = date_create(); foreach( timezone_identifiers_list() as $tz ) { $zone = timezone_open( $tz ); $name = timezone_name_get( $zone ); $location = timezone_location_get( $zone ); $offset = timezone_offset_get( $zone, $now ) / 60; // convert seconds to minutes $zones[] = array('name' => $name, 'offset' => $offset, 'location' => $location); } return $zones; }
function amr_say_when($timestamp, $report = '') { global $tzobj; // if (WP_DEBUG) echo '<br />Timestamp = '.$timestamp; //$d = date_create(strftime('%C-%m-%d %H:%I:%S',$timestamp)); //do not use %c - may be locale issues $d = new datetime('@' . $timestamp); //do not use %c - may be locale issues, wdindows no likely %C if (is_object($d)) { if (!is_object($tzobj)) { amr_getset_timezone(); } date_timezone_set($d, $tzobj); $timetext = $d->format(get_option('date_format') . ' ' . get_option('time_format')); $text = sprintf(__('Cache already scheduled for %s, in %s time', 'amr-users'), $timetext . ' ' . timezone_name_get($tzobj), human_time_diff(time(), $timestamp)); } else { $text = 'Unknown error in formatting timestamp got next cache: ' . $timestamp . ' ' . print_r($d, true); } return $text; }
var_dump(date_format($dt, 'Y-m-d H:i:s')); var_dump(timezone_name_from_abbr("CET")); var_dump(timezone_name_from_abbr("", 3600, 0)); $tz = timezone_open("Asia/Shanghai"); var_dump(timezone_name_get($tz)); // Create two timezone objects, one for Taipei (Taiwan) and one for // Tokyo (Japan) $dateTimeZoneTaipei = timezone_open("Asia/Taipei"); $dateTimeZoneJapan = timezone_open("Asia/Tokyo"); // Create two DateTime objects that will contain the same Unix timestamp, but // have different timezones attached to them. $dateTimeTaipei = date_create("2008-08-08", $dateTimeZoneTaipei); $dateTimeJapan = date_create("2008-08-08", $dateTimeZoneJapan); var_dump(date_offset_get($dateTimeTaipei)); var_dump(date_offset_get($dateTimeJapan)); $tz = timezone_open("Asia/Shanghai"); var_dump(timezone_name_get($tz)); $timezone = timezone_open("CET"); $transitions = timezone_transitions_get($timezone); var_dump($transitions[0]['ts']); var_dump($transitions[0]['offset']); var_dump($transitions[0]['isdst']); var_dump($transitions[0]['abbr']); $tz = timezone_open("EDT"); var_dump(timezone_name_get($tz)); $tz = timezone_open("PST"); var_dump(timezone_name_get($tz)); $tz = timezone_open("CHAST"); var_dump(timezone_name_get($tz)); var_dump((bool) timezone_version_get()); var_dump(timezone_open('sdf'));
<?php /* Prototype : DateTime date_timezone_set ( DateTime $object , DateTimeZone $timezone ) * Description: Sets the time zone for the DateTime object * Source code: ext/date/php_date.c * Alias to functions: DateTime::setTimezone */ echo "*** Testing date_timezone_set() : basic functionality ***\n"; //Set the default time zone date_default_timezone_set("Europe/London"); $datetime = date_create("2009-01-30 17:57:32"); $tz = date_timezone_get($datetime); echo "Default timezone: " . timezone_name_get($tz) . "\n"; $datetime = date_create("2009-01-30 22:57:32"); $la_time = timezone_open("America/Los_Angeles"); date_timezone_set($datetime, $la_time); $tz = date_timezone_get($datetime); echo "New timezone: " . timezone_name_get($tz) . "\n"; ?> ===DONE===
throw new Exception(__('No password given')); } if ($u_pwd != $u_pwd2) { throw new Exception(__("Passwords don't match")); } if (strlen($u_pwd) < 6) { throw new Exception(__('Password must contain at least 6 characters.')); } # Try to guess timezone $default_tz = 'Europe/London'; if (!empty($_POST['u_date']) && function_exists('timezone_open')) { if (preg_match('/\\((.+)\\)$/', $_POST['u_date'], $_tz)) { $_tz = $_tz[1]; $_tz = @timezone_open($_tz); if ($_tz instanceof DateTimeZone) { $_tz = @timezone_name_get($_tz); if ($_tz) { $default_tz = $_tz; } } unset($_tz); } } # Create schema $_s = new dbStruct($core->con, $core->prefix); require dirname(__FILE__) . '/../../inc/dbschema/db-schema.php'; $si = new dbStruct($core->con, $core->prefix); $changes = $si->synchronize($_s); # Create user $cur = $core->con->openCursor($core->prefix . 'user'); $cur->user_id = $u_login;
function amr_format_timezone($tzobj) { global $amr_options; if (is_object($tzobj)) { $tzstring = timezone_name_get($tzobj); } else { $tzstring = $tzobj; } return $tzstring; }
/** * @todo rename and document * * @param \Drupal\calendar\CalendarEvent $event * @return array */ function explode_values($event) { $rows = []; $dateInfo = $this->dateArgument->view->dateInfo; // $item_start_date = $event->date_start; // $item_end_date = $event->date_end; // $to_zone = $event->to_zone; // $db_tz = $event->db_tz; // $granularity = $event->granularity; // $increment = $event->increment; // Now that we have an 'entity' for each view result, we need to remove // anything outside the view date range, and possibly create additional // nodes so that we have a 'node' for each day that this item occupies in // this view. // @TODO make this work with the CalendarDateInfo object // $now = max($dateInfo->min_zone_string, $this->dateFormatter->format($event->getStartDate()->getTimestamp(), 'Y-m-d')); // $to = min($dateInfo->max_zone_string, $this->dateFormatter->format($event->getEndDate()->getTimestamp(), 'Y-m-d')); $now = $event->getStartDate()->format('Y-m-d'); $to = $event->getEndDate()->format('Y-m-d'); $next = new \DateTime(); $next->setTimestamp($event->getStartDate()->getTimestamp()); if (timezone_name_get($this->dateArgument->view->dateInfo->getTimezone()) != $event->getTimezone()->getName()) { // Make $start and $end (derived from $node) use the timezone $to_zone, // just as the original dates do. $next->setTimezone($event->getTimezone()); } if (empty($to) || $now > $to) { $to = $now; } // $now and $next are midnight (in display timezone) on the first day where node will occur. // $to is midnight on the last day where node will occur. // All three were limited by the min-max date range of the view. $position = 0; while (!empty($now) && $now <= $to) { /** @var $entity \Drupal\calendar\CalendarEvent */ $entity = clone $event; // Get start and end of current day. $start = $this->dateFormatter->format($next->getTimestamp(), 'custom', 'Y-m-d H:i:s'); $next->setTimestamp(strtotime(' +1 day -1 second', $next->getTimestamp())); $end = $this->dateFormatter->format($next->getTimestamp(), 'custom', 'Y-m-d H:i:s'); // Get start and end of item, formatted the same way. $item_start = $this->dateFormatter->format($event->getStartDate()->getTimestamp(), 'custom', 'Y-m-d H:i:s'); $item_end = $this->dateFormatter->format($event->getEndDate()->getTimestamp(), 'custom', 'Y-m-d H:i:s'); // Get intersection of current day and the node value's duration (as // strings in $to_zone timezone). $start_string = $item_start < $start ? $start : $item_start; $entity->setStartDate(new \DateTime($start_string)); $end_string = !empty($item_end) ? $item_end > $end ? $end : $item_end : NULL; $entity->setEndDate(new \DateTime($end_string)); // @TODO don't hardcode granularity and increment $granularity = 'hour'; $increment = 1; $entity->setAllDay(CalendarHelper::dateIsAllDay($entity->getStartDate()->format('Y-m-d H:i:s'), $entity->getEndDate()->format('Y-m-d H:i:s'), $granularity, $increment)); $calendar_start = new \DateTime(); $calendar_start->setTimestamp($entity->getStartDate()->getTimestamp()); // unset($entity->calendar_fields); if (isset($entity) && empty($calendar_start)) { // if no date for the node and no date in the item // there is no way to display it on the calendar unset($entity); } else { // $entity->date_id .= '.' . $position; $rows[] = $entity; unset($entity); } $next->setTimestamp(strtotime('+1 second', $next->getTimestamp())); $now = $this->dateFormatter->format($next->getTimestamp(), 'Y-m-d'); $position++; } return $rows; }
/** * Gets the name of the timezone * * @return string name */ public function getName() { return timezone_name_get($this->tz); }
function amr_parseTZID($text) { //also used when editing an event global $amr_globaltz, $globaltzstring, $icsfile_tzname, $icsfile_tz; // the ics timezone object that we last parsed and ended up with (often it's all the same /* take a string that may have a olson tz object and try to return a tz object */ /* accept long and short TZ's, --- assume website tz if not valid eg Zimbra's: GMT+01.00/+02.00 */ $icstzid = trim($text, '"='); /* check for enclosing quotes like zimbra issues */ //----------------------------------- //----------------------------------- is it same as wordpress ? if (empty($globaltzstring)) { $globaltzstring = timezone_name_get($amr_globaltz); } if ($globaltzstring == $icstzid) { /* if the timezone matches the wordpress or shortcode time zone, then we are cool ! */ $icsfile_tzname = $icstzid; // set the global $icsfile_tz = $amr_globaltz; if (isset($_REQUEST['tzdebug'])) { echo '<br />' . $icstzid . ' Matches wordpress tz.'; } return $amr_globaltz; } //----------------------------------- is it same as previously parsed ? if yes, go with that if (!empty($icsfile_tzname) and $icsfile_tzname == $icstzid) { if (isset($_REQUEST['tzdebug'])) { echo '<br />' . $icstzid . ' Matches previously parsed tz.'; } return $icsfile_tz; } //----------------------------------- is it a valid php timezone ? $timezone_identifiers = DateTimeZone::listIdentifiers(); //foreach ($timezone_identifiers as $i=> $z) {echo '<br />'.$i; var_dump($z);} if (in_array($icstzid, $timezone_identifiers, false)) { // we now have a valid php timezone if (isset($_REQUEST['tzdebug'])) { echo '<br/>Php should like:' . $icstzid; } $tz = amr_try_timezone($icstzid); // this should work else simething weird going on if (!empty($tz)) { return $tz; } } //----------------------------------- if not already a valid php timezone, can we make it a valid zone ? $tzid = apply_filters('amr-timezoneid-filter', $icstzid); //apply filters like for windows zones etc // let us see if php likes it try { set_error_handler('amr_tz_error_handler'); /* ignore errors , just giveit a go*/ $tz = timezone_open($tzid); restore_error_handler(); if ($tz) { $tzname = $tzid; if (isset($_REQUEST['tzdebug'])) { echo '<br/>Php liked:' . $tzid; } } else { $tz = amr_deduceTZID($tzid); } } catch (Exception $e) { /* we tried the filter but php did't like, so lets try fix it */ /* else try figure the timezone out */ $tz = amr_deduceTZID($tzid); } if (empty($tz)) { if (isset($_REQUEST['tzdebug'])) { echo '<br/>Giving up on timezone: ' . $icstzid; } return false; // we couldn't figure it out } if (empty($icsfile_tzname)) { // if this is the first time we parsed, lets save the hard work $icsfile_tzname = $icstzid; // what was actually in the file $icsfile_tz = $tz; // this is the php tz we ended up with } return $tz; }
<?php /* Prototype : string timezone_name_get ( DateTime $object ) * Description: Returns the name of the timezone * Source code: ext/date/php_date.c * Alias to functions: DateTimeZone::getName() */ // Set timezone date_default_timezone_set("Europe/London"); echo "*** Testing timezone_name_get() : error conditions ***\n"; echo "\n-- Testing timezone_name_get() function with zero arguments --\n"; var_dump(timezone_name_get()); echo "\n-- Testing date_timezone_set() function with more than expected no. of arguments --\n"; $datetime = date_create("2009-01-30 17:57:32"); $extra_arg = 99; var_dump(timezone_name_get($datetime, $extra_arg)); echo "\n-- Testing timezone_name_get() function with an invalid values for \$object argument --\n"; $invalid_obj = new stdClass(); var_dump(timezone_name_get($invalid_obj)); $invalid_obj = 10; var_dump(timezone_name_get($invalid_obj)); $invalid_obj = null; var_dump(timezone_name_get($invalid_obj)); ?> ===DONE===
protected function timeToUser($dbTime, $formatOut = "Y-m-d H:i", $timestamp = FALSE) { $date = new \DateTime($dbTime); $tz_server = timezone_name_get($date->getTimezone()); if (empty($tz_user) or $tz_user == $tz_server) { return $date->format($formatOut); } $date->setTimezone(new \DateTimeZone($tz_user)); return $date->format($formatOut); }
function amr_check_timezonesettings() { global $amr_globaltz; echo '<ul>'; if (function_exists('timezone_version_get')) { printf('<li>' . __('Your timezone db version is: %s', 'amr-ical-events-list') . '</li>', timezone_version_get()); } else { echo '<li>' . '<a href="http://en.wikipedia.org/wiki/Tz_database">' . __('Plugin cannot determine timezonedb version in php < 5.3.', 'amr-ical-events-list') . '</a>'; } echo '</li></li><li><a href="http://pecl.php.net/package/timezonedb">'; _e('Php timezonedb versions', 'amr-ical-events-list'); echo '</a> <a href="http://pecl.php.net/package/timezonedb">'; _e('Info on what changes are in which timezonedb version', 'amr-ical-events-list'); echo '</a></li>'; if (!isset($amr_globaltz)) { echo '<b>' . __('No global timezone - is there a problem here? ', 'amr-ical-events-list') . '</b>'; return; } $tz = get_option('timezone_string'); $settingslink = '<a href="' . get_option('siteurl') . '/wp-admin/options-general.php">' . __('Go to settings', 'amr-ical-events-list') . '</a>'; if ($tz == '') { $gmtoffset = get_option('gmt_offset'); if (!empty($gmtoffset)) { printf('<li style="color: red;"><b>' . __('You are using the "old" gmt_offset setting ', 'amr-ical-events-list') . '</b></li><li>', $gmtoffset); _e('Consider changing to the more accurate timezone setting', 'amr-ical-events-list'); echo '</li>'; } } $now = date_create('now', $amr_globaltz); echo '<li>' . __('The plugin thinks your timezone is: ', 'amr-ical-events-list') . timezone_name_get($amr_globaltz) . ' ' . $settingslink . '</li>' . '<li>' . __('The current UTC offset for that timezone is: ', 'amr-ical-events-list') . $now->getoffset() / (60 * 60) . '</li>'; if (function_exists('timezone_transitions_get')) { foreach (timezone_transitions_get($amr_globaltz) as $tr) { if ($tr['ts'] > time()) { break; } } } $utctz = new DateTimeZone('UTC'); if (isset($tr['ts'])) { $d = amr_create_date_time("@{$tr['ts']}", $utctz); //try {$d = new DateTime( "@{$tr['ts']}",$utctz );} //catch(Exception $e) { break;} date_timezone_set($d, $amr_globaltz); printf('<li>' . __('Switches to %s on %s. GMT offset: %d', 'amr-ical-events-list') . '</li>', $tr['isdst'] ? "DST" : "standard time", $d->format('d M Y @ H:i'), $tr['offset'] / (60 * 60)); } echo '<li>'; _e('Current time (unlocalised): ', 'amr-ical-events-list'); echo $now->format('r'); echo '</li></ul>'; }