public static function get_nearest_timezone($cur_lat, $cur_long, $country_code = '') { $timezone_ids = $country_code ? DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code) : DateTimeZone::listIdentifiers(); if ($timezone_ids && is_array($timezone_ids) && isset($timezone_ids[0])) { $time_zone = ''; $tz_distance = 0; //only one identifier? if (count($timezone_ids) == 1) { $time_zone = $timezone_ids[0]; } else { foreach ($timezone_ids as $timezone_id) { $timezone = new DateTimeZone($timezone_id); $location = $timezone->getLocation(); $tz_lat = $location['latitude']; $tz_long = $location['longitude']; $theta = $cur_long - $tz_long; $distance = sin(deg2rad($cur_lat)) * sin(deg2rad($tz_lat)) + cos(deg2rad($cur_lat)) * cos(deg2rad($tz_lat)) * cos(deg2rad($theta)); $distance = acos($distance); $distance = abs(rad2deg($distance)); // echo '<br />'.$timezone_id.' '.$distance; if (!$time_zone || $tz_distance > $distance) { $time_zone = $timezone_id; $tz_distance = $distance; } } } return $time_zone; } return 'unknown'; }
/** Return additional information for specified Unix timezone @return array @param $id string @private **/ private static function tzdata($id) { $ref = new DateTimeZone($id); $loc = $ref->getLocation(); $now = time(); $trn = $ref->getTransitions($now, $now); return array('offset' => $ref->getOffset(new DateTime('now', new DateTimeZone('GMT'))) / 3600, 'country' => $loc['country_code'], 'latitude' => $loc['latitude'], 'longitude' => $loc['longitude'], 'dst' => $trn[0]['isdst']); }
/** * Return information about specified Unix time zone * @return array * @param $zone string **/ function tzinfo($zone) { $ref = new \DateTimeZone($zone); $loc = $ref->getLocation(); $trn = $ref->getTransitions($now = time(), $now); $out = array('offset' => $ref->getOffset(new \DateTime('now', new \DateTimeZone('GMT'))) / 3600, 'country' => $loc['country_code'], 'latitude' => $loc['latitude'], 'longitude' => $loc['longitude'], 'dst' => $trn[0]['isdst']); unset($ref); return $out; }
/** * Gets the timezone that is closest to the given coordinates * * @param float $lag * @param float $lng * @return DateTimeZone Timezone object */ public static function timezoneByCoordinates($lat, $lng) { $current = ['timezone' => null, 'distance' => 0]; $identifiers = DateTimeZone::listIdentifiers(); foreach ($identifiers as $identifier) { $timezone = new DateTimeZone($identifier); $location = $timezone->getLocation(); $point = ['lat' => $location['latitude'], 'lng' => $location['longitude']]; $distance = (int) GeocodeLib::calculateDistance(compact('lat', 'lng'), $point); if (!$current['distance'] || $distance < $current['distance']) { $current = ['timezone' => $identifier, 'distance' => $distance]; } } return $current['timezone']; }
public static function timezone($latitude, $longitude) { $alltimezones = DateTimeZone::listIdentifiers(); $variances = array(); //calculate for all timezones the system know foreach ($alltimezones as $timezone) { $datetimezoneobj = new DateTimeZone($timezone); $locationinformations = $datetimezoneobj->getLocation(); $latitudeoftimezone = $locationinformations['latitude']; $longitudeoftimezone = $locationinformations['longitude']; $variances[abs($latitudeoftimezone - $latitude) + abs($longitudeoftimezone - $longitude)] = $timezone; } //sort array and return the timezone with the smallest difference ksort($variances); reset($variances); return current($variances); }
/** * Attempts to find the closest timezone by coordinates * * @static * @param $lat * @param $lng */ function getClosestTimezone($lat, $lng) { $diffs = array(); foreach (DateTimeZone::listIdentifiers() as $timezoneID) { $timezone = new DateTimeZone($timezoneID); $location = $timezone->getLocation(); $tLat = $location['latitude']; $tLng = $location['longitude']; $diffLat = abs($lat - $tLat); $diffLng = abs($lng - $tLng); $diff = $diffLat + $diffLng; $diffs[$timezoneID] = $diff; } //asort($diffs); $timezone = array_keys($diffs, min($diffs)); return $timezone[0]; }
function calculate_sun_rise_set() { $tzone = date_default_timezone_get(); if ($tzone == '') { $tzone = 'Europe/Berlin'; } $tz = new DateTimeZone($tzone); $loc = $tz->getLocation(); $sun_info = date_sun_info(time(), $loc['latitude'], $loc['longitude']); //returns sunrise, sunset, ... //echo "Sunrise: " . date("H:i:s", $sun_info['sunrise']) . "\n"; //echo "Sunset: " . date("H:i:s", $sun_info['sunset']) . "\n"; //update sunrise $sql = query("SELECT id,suninfo,name FROM timer WHERE suninfo='sunset' OR suninfo='sunrise'"); while ($row = fetch($sql)) { //echo $row['suninfo'] . $row['name'] ; if ($row['suninfo'] == 'sunrise') { $sqlset = query("UPDATE timer SET time = '" . date("H:i", $sun_info['sunrise']) . "', hour ='" . date("H", $sun_info['sunrise']) . "', minute ='" . date("i", $sun_info['sunrise']) . "' WHERE id = '" . $row['id'] . "'"); } if ($row['suninfo'] == 'sunset') { $sqlset = query("UPDATE timer SET time = '" . date("H:i", $sun_info['sunset']) . "', hour ='" . date("H", $sun_info['sunset']) . "', minute ='" . date("i", $sun_info['sunset']) . "' WHERE id = '" . $row['id'] . "'"); } } }
/** * * @return string $timezone */ public static function findTimezone() { $locale = self::getLocale(); if (!$locale->getRegion()) { try { $locale = new Zend_Locale($locale . '_' . strtoupper($locale)); } catch (Exception $e) { } } $region = $locale->getRegion(); if ($region) { $timezoneList = Zend_Locale::getTranslationList('TimezoneToTerritory'); if (isset($timezoneList[$region])) { $timezone = $timezoneList[$region]; return $timezone; } } $httpCookieObject = new Zend_Http_Cookie('timezone', null, Zmz_Host::getHostname()); $cookie = new Zmz_Cookie($httpCookieObject); $timezoneCookieValue = $cookie->getValue(); $timezoneArray = @explode('/', $timezoneCookieValue); // check valid cookie if (isset($timezoneArray[0]) && isset($timezoneArray[1])) { $offset = intval($timezoneArray[0]); $dts = intval($timezoneArray[1]); $guessedTimezone = timezone_name_from_abbr('', $offset, $dts); try { $tz = new DateTimeZone($guessedTimezone); $guessedLocation = $tz->getLocation(); // $guessedLocationCode = $guessedLocation['country_code']; $timezone = $guessedTimezone; } catch (Exception $e) { $timezone = self::$defaultTimezone; } } else { $timezone = self::$defaultTimezone; } return $timezone; }
echo "<br/>"; $winter = new DateTime('2010-12-21', new DateTimeZone('America/New_York')); $summer = new DateTime('2008-06-21', new DateTimeZone('America/New_York')); echo $winter->getOffset() . "\n"; echo $summer->getOffset() . "\n"; echo "<br/>"; $date = new DateTime(); echo $date->getTimestamp(); echo "<br/>"; $date = new DateTime(null, new DateTimeZone('Europe/London')); $tz = $date->getTimezone(); echo $tz->getName(); echo "<br/>"; echo "<pre>"; $tz = new DateTimeZone("Europe/Prague"); print_r($tz->getLocation()); print_r(timezone_location_get($tz)); //checkdate用于验证一个验证一个格里高里日期(就是公历) var_dump(checkdate(12, 31, 2000)); var_dump(checkdate(2, 29, 2001)); echo "<br/>"; echo "<pre>"; print_r(date_parse("2006-12-12 10:00:00.5")); echo "<br/>"; echo "<pre>"; $today = getdate(); print_r($today); //返回Uinx时间戳 echo "<br/>"; //hour,minute,second,month,day,year echo mktime(0, 0, 0, 1, 1, 2000);
<?php $timezone = new DateTimeZone('Europe/Rome'); echo $timezone->getName(); print_r($timezone->getLocation()); ?>
/** * Convert an event object to be used on the client */ private function _client_event($event, $addcss = false) { $cal_tz = $this->timezone->getName(); if (!$event['allday'] && $event['tzname'] && $cal_tz != $event['tzname']) { $cal_tz = new DateTimeZone($cal_tz); $event_tz = new DateTimeZone($event['tzname']); $event['tzinfo'] = $event_tz->getLocation(); $cal_dt = new DateTime(date('Y-m-d H:i:s', $event['start']->format('U')), $cal_tz); $event_dt = new DateTime(date('Y-m-d H:i:s', $event['start']->format('U')), $event_tz); $event['tzadjust'] = -($cal_dt->getOffset() - $event_dt->getOffset()); } else { $event['tzadjust'] = 0; } // compose a human readable strings for alarms_text and recurrence_text if ($event['alarms']) { $event['alarms_text'] = libcalendaring::alarms_text($event['alarms']); } if ($event['recurrence']) { $event['recurrence_text'] = $this->_recurrence_text($event['recurrence']); if ($event['recurrence']['UNTIL']) { $event['recurrence']['UNTIL'] = $this->lib->adjust_timezone($event['recurrence']['UNTIL'], $event['allday'])->format('c'); } unset($event['recurrence']['EXCEPTIONS']); // format RDATE values if (is_array($event['recurrence']['RDATE'])) { $libcal = $this->lib; $event['recurrence']['RDATE'] = array_map(function ($rdate) use($libcal) { return $libcal->adjust_timezone($rdate, true)->format('c'); }, $event['recurrence']['RDATE']); } } foreach ((array) $event['attachments'] as $k => $attachment) { $event['attachments'][$k]['classname'] = rcube_utils::file2class($attachment['mimetype'], $attachment['name']); } // check for organizer in attendees list $organizer = null; foreach ((array) $event['attendees'] as $i => $attendee) { if (isset($attendee['role']) && $attendee['role'] == 'ORGANIZER') { $organizer = $attendee; break; } } if ($organizer === null && !empty($event['organizer'])) { $organizer = $event['organizer']; $organizer['role'] = 'ORGANIZER'; if (!is_array($event['attendees'])) { $event['attendees'] = array(); } array_unshift($event['attendees'], $organizer); } // mapping url => vurl because of the fullcalendar client script $event['vurl'] = $event['url']; unset($event['url']); // Begin mod by Rosali (https://issues.kolab.org/show_bug.cgi?id=3481) // Fix 1 second issue of all-day events if ($event['allday'] && isset($event['end'])) { if ($event['start'] == $event['end']) { $event['end']->modify('+ 1 day'); $event['end']->modify('- 1 minute'); } } $start = $event['start'] ? $event['start']->format('c') : null; $end = $event['end'] ? $event['end']->format('c') : null; if (!$event['allday']) { if (isset($event['end'])) { $estart = $event['start']->format('U'); $eend = $event['end']->format('U'); if ($eend - $estart > $this->rc->config->get('calendar_treat_as_allday', 6) * 3600) { $view_start = get_input_value('start', RCUBE_INPUT_GPC); $view_end = get_input_value('end', RCUBE_INPUT_GPC); $event['allday'] = true; $event['allDayfake'] = true; if ($event['start']->format('U') >= $view_start) { $event['left'] = $event['start']->format($this->rc->config->get('time_format', 'H:i')); } else { $event['left'] = ''; } if ($event['end']->format('U') <= $view_end) { $event['right'] = $event['end']->format($this->rc->config->get('time_format', 'H:i')); } else { $event['right'] = ''; } } } } $tempClass = $event['temp'] ? 'fc-event-temp ' : ''; // End mod by Rosali // Begin mod by Rosali (advanced categories colorizing) if (is_string($event['categories'])) { $event['categories'] = explode(',', $event['categories']); foreach ($event['categories'] as $idx => $cat) { $event['categories'][$idx] = trim($cat); } } $readwrite_color = $this->rc->config->get('calendar_events_default_background_color', $this->defaults['calendar_events_default_background_color']); $readonly_color = $this->rc->config->get('calendar_readonly_events_default_background_color', $this->defaults['calendar_event_defaults_background_color']); $mode = $this->rc->config->get('calendar_event_coloring', $this->defaults['calendar_event_coloring']); $categories = $this->rc->config->get('calendar_categories', array()); $calendars = $this->get_calendars(); $calendar_color = $calendars[$event['calendar']]['color']; foreach ((array) $event['categories'] as $idx => $category) { $category_color = $event['readonly'] ? $readonly_color : $readwrite_color; if (isset($categories[$event['categories'][$idx]])) { $category_color = $categories[$category]; break; } } switch ($mode) { case '0': $backgroundColor = $calendar_color; $borderColor = $calendar_color; break; case '1': $backgroundColor = $category_color; $borderColor = $category_color; break; case '2': $backgroundColor = $category_color; $borderColor = $calendar_color; break; case '3': $backgroundColor = $calendar_color; $borderColor = $category_color; break; } $mode = $this->rc->config->get('calendar_event_font_color', 0); if ($mode == 0) { $c_r = hexdec(substr($backgroundColor, 0, 2)); $c_g = hexdec(substr($backgroundColor, 2, 2)); $c_b = hexdec(substr($backgroundColor, 4, 2)); $brightness = ($c_r * 299 + $c_g * 587 + $c_b * 114) / 1000; if ($brightness > 130) { $fontColor = '000000'; } else { $fontColor = 'FFFFFF'; } } else { if ($mode == 1) { $fontColor = substr(dechex(~hexdec($backgroundColor)), -6); } else { if ($mode == 2) { $fontColor = 'FFFFFF'; } else { $fontColor = '000000'; } } } // End mod by Rosali return array('_id' => $event['calendar'] . ':' . $event['id'], 'start' => $start, 'end' => $end, 'changed' => $event['changed'] ? $this->lib->adjust_timezone($event['changed'])->format('c') : null, 'created' => $event['created'] ? $this->lib->adjust_timezone($event['created'])->format('c') : null, 'title' => strval($event['title']), 'description' => strval($event['description']), 'location' => strval($event['location']), 'categories' => is_array($event['categories']) ? implode(', ', $event['categories']) : $event['categories'], 'className' => ($addcss ? 'fc-event-cal-' . asciiwords($event['calendar'], true) . ' ' : '') . $tempClass, 'calendarColor' => $calendar_color, 'categoryColor' => $category_color, 'backgroundColor' => '#' . $backgroundColor, 'borderColor' => '#' . $borderColor, 'textColor' => '#' . $fontColor, 'allDay' => $event['allday'] == 1) + $event; }
<?php $identifiers = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'IR'); echo '<p>' . count($identifiers) . ' identifiers found:</p>'; echo '<ul>'; foreach ($identifiers as $id) { echo '<li>'; echo $id; $tz = new DateTimeZone($id); $comments = $tz->getLocation()['comments']; if ($comments) { echo ' : ' . $comments; } echo '</li>'; } echo '</ul>';
/** * This function returns an array with the most likely latitude and longitud. * We can get the Lat and Lng from Php's TimeZone object. We can also get it * from the ipSearch function here which would be more accurate. */ public function getLatLng() { // do we have an api key for the IP? if (Phpfox::getParam('core.ip_infodb_api_key') != '') { $aInfo = $this->ipSearch(Phpfox_Request::instance()->getServer('REMOTE_ADDR')); // $this->ipSearch($_SERVER['REMOTE_ADDR']); if (isset($aInfo[Phpfox::getPhrase('admincp.longitude')]) && !empty($aInfo[Phpfox::getPhrase('admincp.longitude')]) && isset($aInfo[Phpfox::getPhrase('admincp.latitude')]) && !empty($aInfo[Phpfox::getPhrase('admincp.latitude')])) { return array('latitude' => $aInfo[Phpfox::getPhrase('admincp.latitude')], 'longitude' => $aInfo[Phpfox::getPhrase('admincp.longitude')]); } } // has user set a country if (($sTz = Phpfox::getUserBy('time_zone')) != '' && PHPFOX_USE_DATE_TIME) { $aTZ = $this->getTimeZones(); if (isset($aTZ[$sTz])) { $oTz = new DateTimeZone($aTZ[$sTz]); $aInfo = $oTz->getLocation(); return array('latitude' => $aInfo['latitude'], 'longitude' => $aInfo['longitude']); } } /* return a default value (London GMT0)*/ return array('latitude' => '51.544627', 'longitude' => '-0.184021'); }
/** * Get location information for a timezone, including country_code, latitude, longitude and comments. * @param string $timezoneIdentifier * @return array */ public static function getGeolocationFromTimezone($timezoneIdentifier) { $tz = new DateTimeZone($timezoneIdentifier); return $tz->getLocation(); }
<?php $timeZone = ini_get('date.timezone'); $dtz = new DateTimeZone($timeZone); echo "Server's Time Zone: " . $timeZone . "<br/>"; foreach ($dtz->getLocation() as $key => $value) { echo $key . " " . $value . "<br/>"; }