* string(7) "Caption" * [1] => * string(20) "(UTC+09:30) Adelaide" * [2] => * string(0) "" * } */ exec("wmic timezone get Caption", $output); $string = trim(implode("\n", $output)); //Detect the Time Zone string preg_match($regex, $string, $matches); if(!isset($matches[2])){ return false; } $offset = $matches[2]; if($offset == ""){ return "UTC"; } return self::parseOffset($offset); case 'linux': // Ubuntu / Debian. if(file_exists('/etc/timezone')){ $data = file_get_contents('/etc/timezone'); if($data){ return trim($data); } } // RHEL / CentOS if(file_exists('/etc/sysconfig/clock')){ $data = parse_ini_file('/etc/sysconfig/clock'); if(!empty($data['ZONE'])){ return trim($data['ZONE']); } } //Portable method for incompatible linux distributions. $offset = trim(exec('date +%:z')); if($offset == "+00:00"){ return "UTC"; } return self::parseOffset($offset); case 'mac': if(is_link('/etc/localtime')){ $filename = readlink('/etc/localtime'); if(strpos($filename, '/usr/share/zoneinfo/') === 0){ $timezone = substr($filename, 20); return trim($timezone); } } return false; default: return false; } } /** * @param string $offset In the format of +09:00, +02:00, -04:00 etc. * * @return string|bool */ private static function parseOffset($offset){ //Make signed offsets unsigned for date_parse if(strpos($offset, '-') !== false){ $negative_offset = true; $offset = str_replace('-', '', $offset); }else{ if(strpos($offset, '+') !== false){ $negative_offset = false; $offset = str_replace('+', '', $offset); }else{ return false; } } $parsed = date_parse($offset); $offset = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second']; //After date_parse is done, put the sign back if($negative_offset == true){ $offset = -abs($offset); } //And then, look the offset up. //timezone_name_from_abbr is not used because it returns false on some(most) offsets because it's mapping function is weird. //That's been a bug in PHP since 2008! foreach(timezone_abbreviations_list() as $zones){ foreach($zones as $timezone){ if($timezone['offset'] == $offset){ return $timezone['timezone_id']; } } } return false; } }