PHP5.2.4之前的版本无需设置时区。下面是修改PHP时区的三个办法。
1、修改PHP.ini这个文件
找到date.timezone这行,去掉前面的分号,改成:
Java代码
- date.timezone = Asia/Shanghai
2、修改 .htaccess文件
修改.htaccess文件有两种办法,下面的两条语句只要有一条即可
Java代码
- php_value date.timezone Asia/Shanghai
- SetEnv TZ Asia/Shanghai
3、修改PHP代码
也是下面的两条语句只要其中的一句即可
Php代码
- date_default_timezone_set(‘Asia/Shanghai‘);
- ini_set(‘date.timezone‘,‘Asia/Shanghai‘);
例如: <?php date_default_timezone_set(‘Asia/Shanghai‘); ?>
Php代码
- <?php
- include(‘tz.php‘);
- echo get_localtime(time(),‘Asia/Hong_Kong‘); // local time
- //get_alltime(time()); // global time
- ?>
具体见附件;
例外,也收集了一些有关时区的动动;
1. PHP TimeZone Array
Php代码
- <?php
- $timezoneTable = array(
- "-12" => "(GMT -12:00) Eniwetok, Kwajalein",
- "-11" => "(GMT -11:00) Midway Island, Samoa",
- "-10" => "(GMT -10:00) Hawaii",
- "-9" => "(GMT -9:00) Alaska",
- "-8" => "(GMT -8:00) Pacific Time (US & Canada)",
- "-7" => "(GMT -7:00) Mountain Time (US & Canada)",
- "-6" => "(GMT -6:00) Central Time (US & Canada), Mexico City",
- "-5" => "(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima",
- "-4" => "(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz",
- "-3.5" => "(GMT -3:30) Newfoundland",
- "-3" => "(GMT -3:00) Brazil, Buenos Aires, Georgetown",
- "-2" => "(GMT -2:00) Mid-Atlantic",
- "-1" => "(GMT -1:00 hour) Azores, Cape Verde Islands",
- "0" => "(GMT) Western Europe Time, London, Lisbon, Casablanca",
- "1" => "(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris",
- "2" => "(GMT +2:00) Kaliningrad, South Africa",
- "3" => "(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg",
- "3.5" => "(GMT +3:30) Tehran",
- "4" => "(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi",
- "4.5" => "(GMT +4:30) Kabul",
- "5" => "(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent",
- "5.5" => "(GMT +5:30) Bombay, Calcutta, Madras, New Delhi",
- "6" => "(GMT +6:00) Almaty, Dhaka, Colombo",
- "7" => "(GMT +7:00) Bangkok, Hanoi, Jakarta",
- "8" => "(GMT +8:00) Beijing, Perth, Singapore, Hong Kong",
- "9" => "(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk",
- "9.5" => "(GMT +9:30) Adelaide, Darwin",
- "10" => "(GMT +10:00) Eastern Australia, Guam, Vladivostok",
- "11" => "(GMT +11:00) Magadan, Solomon Islands, New Caledonia",
- "12" => "(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka"
- );
Php代码
- $timezones = array(
- ‘-12‘=>‘Pacific/Kwajalein‘,
- ‘-11‘=>‘Pacific/Samoa‘,
- ‘-10‘=>‘Pacific/Honolulu‘,
- ‘-9‘=>‘America/Juneau‘,
- ‘-8‘=>‘America/Los_Angeles‘,
- ‘-7‘=>‘America/Denver‘,
- ‘-6‘=>‘America/Mexico_City‘,
- ‘-5‘=>‘America/New_York‘,
- ‘-4‘=>‘America/Caracas‘,
- ‘-3.5‘=>‘America/St_Johns‘,
- ‘-3‘=>‘America/Argentina/Buenos_Aires‘,
- ‘-2‘=>‘Atlantic/Azores‘,// no cities here so just picking an hour ahead
- ‘-1‘=>‘Atlantic/Azores‘,
- ‘0‘=>‘Europe/London‘,
- ‘1‘=>‘Europe/Paris‘,
- ‘2‘=>‘Europe/Helsinki‘,
- ‘3‘=>‘Europe/Moscow‘,
- ‘3.5‘=>‘Asia/Tehran‘,
- ‘4‘=>‘Asia/Baku‘,
- ‘4.5‘=>‘Asia/Kabul‘,
- ‘5‘=>‘Asia/Karachi‘,
- ‘5.5‘=>‘Asia/Calcutta‘,
- ‘6‘=>‘Asia/Colombo‘,
- ‘7‘=>‘Asia/Bangkok‘,
- ‘8‘=>‘Asia/Singapore‘,
- ‘9‘=>‘Asia/Tokyo‘,
- ‘9.5‘=>‘Australia/Darwin‘,
- ‘10‘=>‘Pacific/Guam‘,
- ‘11‘=>‘Asia/Magadan‘,
- ‘12‘=>‘Asia/Kamchatka‘
- );
Java代码
- $zones = array(
- "Pacific/Midway" => "(GMT-11:00) Midway Island, Samoa",
- "America/Adak" => "(GMT-10:00) Hawaii-Aleutian",
- "Etc/GMT+10" => "(GMT-10:00) Hawaii",
- "Pacific/Marquesas" => "(GMT-09:30) Marquesas Islands",
- "Pacific/Gambier" => "(GMT-09:00) Gambier Islands",
- "America/Anchorage" => "(GMT-09:00) Alaska",
- "America/Ensenada" => "(GMT-08:00) Tijuana, Baja California",
- "Etc/GMT+8" => "(GMT-08:00) Pitcairn Islands",
- "America/Los_Angeles" => "(GMT-08:00) Pacific Time (US & Canada)",
- "America/Denver" => "(GMT-07:00) Mountain Time (US & Canada)",
- "America/Chihuahua" => "(GMT-07:00) Chihuahua, La Paz, Mazatlan",
- "America/Dawson_Creek" => "(GMT-07:00) Arizona",
- "America/Belize" => "(GMT-06:00) Saskatchewan, Central America",
- "America/Cancun" => "(GMT-06:00) Guadalajara, Mexico City, Monterrey",
- "Chile/EasterIsland" => "(GMT-06:00) Easter Island",
- "America/Chicago" => "(GMT-06:00) Central Time (US & Canada)",
- "America/New_York" => "(GMT-05:00) Eastern Time (US & Canada)",
- "America/Havana" => "(GMT-05:00) Cuba",
- "America/Bogota" => "(GMT-05:00) Bogota, Lima, Quito, Rio Branco",
- "America/Caracas" => "(GMT-04:30) Caracas",
- "America/Santiago" => "(GMT-04:00) Santiago",
- "America/La_Paz" => "(GMT-04:00) La Paz",
- "Atlantic/Stanley" => "(GMT-04:00) Faukland Islands",
- "America/Campo_Grande" => "(GMT-04:00) Brazil",
- "America/Goose_Bay" => "(GMT-04:00) Atlantic Time (Goose Bay)",
- "America/Glace_Bay" => "(GMT-04:00) Atlantic Time (Canada)",
- "America/St_Johns" => "(GMT-03:30) Newfoundland",
- "America/Araguaina" => "(GMT-03:00) UTC-3",
- "America/Montevideo" => "(GMT-03:00) Montevideo",
- "America/Miquelon" => "(GMT-03:00) Miquelon, St. Pierre",
- "America/Godthab" => "(GMT-03:00) Greenland",
- "America/Argentina/Buenos_Aires" => "(GMT-03:00) Buenos Aires",
- "America/Sao_Paulo" => "(GMT-03:00) Brasilia",
- "America/Noronha" => "(GMT-02:00) Mid-Atlantic",
- "Atlantic/Cape_Verde" => "(GMT-01:00) Cape Verde Is",
- "Atlantic/Azores" => "(GMT-01:00) Azores",
- "Europe/Belfast" => "(GMT) Greenwich Mean Time : Belfast",
- "Europe/Dublin" => "(GMT) Greenwich Mean Time : Dublin",
- "Europe/Lisbon" => "(GMT) Greenwich Mean Time : Lisbon",
- "Europe/London" => "(GMT) Greenwich Mean Time : London",
- "Africa/Abidjan" => "(GMT) Monrovia, Reykjavik",
- "Europe/Amsterdam" => "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna",
- "Europe/Belgrade" => "(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague",
- "Europe/Brussels" => "(GMT+01:00) Brussels, Copenhagen, Madrid, Paris",
- "Africa/Algiers" => "(GMT+01:00) West Central Africa",
- "Africa/Windhoek" => "(GMT+01:00) Windhoek",
- "Asia/Beirut" => "(GMT+02:00) Beirut",
- "Africa/Cairo" => "(GMT+02:00) Cairo",
- "Asia/Gaza" => "(GMT+02:00) Gaza",
- "Africa/Blantyre" => "(GMT+02:00) Harare, Pretoria",
- "Asia/Jerusalem" => "(GMT+02:00) Jerusalem",
- "Europe/Minsk" => "(GMT+02:00) Minsk",
- "Asia/Damascus" => "(GMT+02:00) Syria",
- "Europe/Moscow" => "(GMT+03:00) Moscow, St. Petersburg, Volgograd",
- "Africa/Addis_Ababa" => "(GMT+03:00) Nairobi",
- "Asia/Tehran" => "(GMT+03:30) Tehran",
- "Asia/Dubai" => "(GMT+04:00) Abu Dhabi, Muscat",
- "Asia/Yerevan" => "(GMT+04:00) Yerevan",
- "Asia/Kabul" => "(GMT+04:30) Kabul",
- "Asia/Yekaterinburg" => "(GMT+05:00) Ekaterinburg",
- "Asia/Tashkent" => "(GMT+05:00) Tashkent",
- "Asia/Kolkata" => "(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi",
- "Asia/Katmandu" => "(GMT+05:45) Kathmandu",
- "Asia/Dhaka" => "(GMT+06:00) Astana, Dhaka",
- "Asia/Novosibirsk" => "(GMT+06:00) Novosibirsk",
- "Asia/Rangoon" => "(GMT+06:30) Yangon (Rangoon)",
- "Asia/Bangkok" => "(GMT+07:00) Bangkok, Hanoi, Jakarta",
- "Asia/Krasnoyarsk" => "(GMT+07:00) Krasnoyarsk",
- "Asia/Hong_Kong" => "(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi",
- "Asia/Irkutsk" => "(GMT+08:00) Irkutsk, Ulaan Bataar",
- "Australia/Perth" => "(GMT+08:00) Perth",
- "Australia/Eucla" => "(GMT+08:45) Eucla",
- "Asia/Tokyo" => "(GMT+09:00) Osaka, Sapporo, Tokyo",
- "Asia/Seoul" => "(GMT+09:00) Seoul",
- "Asia/Yakutsk" => "(GMT+09:00) Yakutsk",
- "Australia/Adelaide" => "(GMT+09:30) Adelaide",
- "Australia/Darwin" => "(GMT+09:30) Darwin",
- "Australia/Brisbane" => "(GMT+10:00) Brisbane",
- "Australia/Hobart" => "(GMT+10:00) Hobart",
- "Asia/Vladivostok" => "(GMT+10:00) Vladivostok",
- "Australia/Lord_Howe" => "(GMT+10:30) Lord Howe Island",
- "Etc/GMT-11" => "(GMT+11:00) Solomon Is, New Caledonia",
- "Asia/Magadan" => "(GMT+11:00) Magadan",
- "Pacific/Norfolk" => "(GMT+11:30) Norfolk Island",
- "Asia/Anadyr" => "(GMT+12:00) Anadyr, Kamchatka",
- "Pacific/Auckland" => "(GMT+12:00) Auckland, Wellington",
- "Etc/GMT-12" => "(GMT+12:00) Fiji, Kamchatka, Marshall Is",
- "Pacific/Chatham" => "(GMT+12:45) Chatham Islands",
- "Pacific/Tongatapu" => "(GMT+13:00) Nuku‘alofa",
- "Pacific/Kiritimati" => "(GMT+14:00) Kiritimati"
- );
2. 获取GMT标准时间
Php代码
- echo ‘GMT :‘.date("m/d/Y H:i", time() - date("Z"));
3. GMT to local time
下面的程序结合上面两条,将更加有用。
Java代码
- <?php
- // I am using the convention (assumption) of "07/04/2004 14:45"
- $processdate = "12/04/2010 14:45"; // date("m/d/Y H:i", time() - date("Z")) is GMT
- // gmttolocal is a function
- // i am passing it 2 parameters:
- // 1)the date in the above format and
- // 2)time difference as a number; -5 in our case (GMT to CDT)
- echo gmttolocal($processdate,-5);
- function gmttolocal($mydate,$mydifference)
- {
- // trying to seperate date and time
- $datetime = explode(" ",$mydate);
- // trying to seperate different elements in a date
- $dateexplode = explode("/",$datetime[0]);
- // trying to seperate different elements in time
- $timeexplode = explode(":",$datetime[1]);
- // getting the unix datetime stamp
- $unixdatetime = mktime($timeexplode[0]+$mydifference,$timeexplode[1],0,$dateexplode[0],$dateexplode[1],$dateexplode[2]);
- // return the local date
- return date("m/d/Y H:i",$unixdatetime);
- }
什么是GMT?
格林尼治标准时间(GMT)
由来
由于地球轨道并非圆形,其运行速度又随着地球与太阳的距离改变而出现变化,因此视太阳时欠缺均匀性。视太阳日的长度同时亦受到地球自转轴相对轨道面的倾 斜度所影响。为着要纠正上述的不均匀性,天文学家计算地球非圆形轨迹与极轴倾斜对视太阳时的效应。平太阳时就是指经修订后的视太阳时。在格林尼治子午线上 的平太阳时称为世界时(UT0),又叫格林尼治平时(GMT)。
北京时间与GMT时间关系
GMT是中央时区,北京在东8区,相差8个小时 所以北京时间=GMT时间+八小时 Greenwich mean time 【天】格林尼治标准时间
拓展小链接
http://geolite.maxmind.com/download/geoip/database/
http://codeigniter.com/forums/viewthread/110099/
另外也可以参看: php获得可靠的精准的当前时间 ( 通过授时服务器 )