php获取当地时间 time zone

 

PHP5.2.4之前的版本无需设置时区。下面是修改PHP时区的三个办法。

1、修改PHP.ini这个文件
找到date.timezone这行,去掉前面的分号,改成:

Java代码 技术分享

  1. date.timezone = Asia/Shanghai 

2、修改 .htaccess文件
修改.htaccess文件有两种办法,下面的两条语句只要有一条即可

Java代码 技术分享

  1. php_value date.timezone Asia/Shanghai 
  2. SetEnv TZ Asia/Shanghai 

3、修改PHP代码
也是下面的两条语句只要其中的一句即可

Php代码 技术分享

  1. date_default_timezone_set(‘Asia/Shanghai‘); 
  2. ini_set(‘date.timezone‘,‘Asia/Shanghai‘); 

例如: <?php date_default_timezone_set(‘Asia/Shanghai‘); ?>

Php代码 技术分享

  1. <?php 
  2. include(‘tz.php‘); 
  3. echo get_localtime(time(),‘Asia/Hong_Kong‘); // local time
  4. //get_alltime(time()); // global time
  5. ?> 

具体见附件;

例外,也收集了一些有关时区的动动;

1. PHP TimeZone Array

Php代码 技术分享

  1. <?php 
  2. $timezoneTable = array( 
  3. "-12" => "(GMT -12:00) Eniwetok, Kwajalein", 
  4. "-11" => "(GMT -11:00) Midway Island, Samoa", 
  5. "-10" => "(GMT -10:00) Hawaii", 
  6. "-9" => "(GMT -9:00) Alaska", 
  7. "-8" => "(GMT -8:00) Pacific Time (US &amp; Canada)", 
  8. "-7" => "(GMT -7:00) Mountain Time (US &amp; Canada)", 
  9. "-6" => "(GMT -6:00) Central Time (US &amp; Canada), Mexico City", 
  10. "-5" => "(GMT -5:00) Eastern Time (US &amp; Canada), Bogota, Lima", 
  11. "-4" => "(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz", 
  12. "-3.5" => "(GMT -3:30) Newfoundland", 
  13. "-3" => "(GMT -3:00) Brazil, Buenos Aires, Georgetown", 
  14. "-2" => "(GMT -2:00) Mid-Atlantic", 
  15. "-1" => "(GMT -1:00 hour) Azores, Cape Verde Islands", 
  16. "0" => "(GMT) Western Europe Time, London, Lisbon, Casablanca", 
  17. "1" => "(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris", 
  18. "2" => "(GMT +2:00) Kaliningrad, South Africa", 
  19. "3" => "(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg", 
  20. "3.5" => "(GMT +3:30) Tehran", 
  21. "4" => "(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi", 
  22. "4.5" => "(GMT +4:30) Kabul", 
  23. "5" => "(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent", 
  24. "5.5" => "(GMT +5:30) Bombay, Calcutta, Madras, New Delhi", 
  25. "6" => "(GMT +6:00) Almaty, Dhaka, Colombo", 
  26. "7" => "(GMT +7:00) Bangkok, Hanoi, Jakarta", 
  27. "8" => "(GMT +8:00) Beijing, Perth, Singapore, Hong Kong", 
  28. "9" => "(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk", 
  29. "9.5" => "(GMT +9:30) Adelaide, Darwin", 
  30. "10" => "(GMT +10:00) Eastern Australia, Guam, Vladivostok", 
  31. "11" => "(GMT +11:00) Magadan, Solomon Islands, New Caledonia", 
  32. "12" => "(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka"
  33. ); 

Php代码 技术分享

  1. $timezones = array( 
  2. ‘-12‘=>‘Pacific/Kwajalein‘, 
  3. ‘-11‘=>‘Pacific/Samoa‘, 
  4. ‘-10‘=>‘Pacific/Honolulu‘, 
  5. ‘-9‘=>‘America/Juneau‘, 
  6. ‘-8‘=>‘America/Los_Angeles‘, 
  7. ‘-7‘=>‘America/Denver‘, 
  8. ‘-6‘=>‘America/Mexico_City‘, 
  9. ‘-5‘=>‘America/New_York‘, 
  10. ‘-4‘=>‘America/Caracas‘, 
  11. ‘-3.5‘=>‘America/St_Johns‘, 
  12. ‘-3‘=>‘America/Argentina/Buenos_Aires‘, 
  13. ‘-2‘=>‘Atlantic/Azores‘,// no cities here so just picking an hour ahead
  14. ‘-1‘=>‘Atlantic/Azores‘, 
  15. ‘0‘=>‘Europe/London‘, 
  16. ‘1‘=>‘Europe/Paris‘, 
  17. ‘2‘=>‘Europe/Helsinki‘, 
  18. ‘3‘=>‘Europe/Moscow‘, 
  19. ‘3.5‘=>‘Asia/Tehran‘, 
  20. ‘4‘=>‘Asia/Baku‘, 
  21. ‘4.5‘=>‘Asia/Kabul‘, 
  22. ‘5‘=>‘Asia/Karachi‘, 
  23. ‘5.5‘=>‘Asia/Calcutta‘, 
  24. ‘6‘=>‘Asia/Colombo‘, 
  25. ‘7‘=>‘Asia/Bangkok‘, 
  26. ‘8‘=>‘Asia/Singapore‘, 
  27. ‘9‘=>‘Asia/Tokyo‘, 
  28. ‘9.5‘=>‘Australia/Darwin‘, 
  29. ‘10‘=>‘Pacific/Guam‘, 
  30. ‘11‘=>‘Asia/Magadan‘, 
  31. ‘12‘=>‘Asia/Kamchatka‘
  32. ); 

Java代码 技术分享

  1. $zones = array( 
  2. "Pacific/Midway"                 => "(GMT-11:00) Midway Island, Samoa", 
  3. "America/Adak"                   => "(GMT-10:00) Hawaii-Aleutian", 
  4. "Etc/GMT+10"                     => "(GMT-10:00) Hawaii", 
  5. "Pacific/Marquesas"              => "(GMT-09:30) Marquesas Islands", 
  6. "Pacific/Gambier"                => "(GMT-09:00) Gambier Islands", 
  7. "America/Anchorage"              => "(GMT-09:00) Alaska", 
  8. "America/Ensenada"               => "(GMT-08:00) Tijuana, Baja California", 
  9. "Etc/GMT+8"                      => "(GMT-08:00) Pitcairn Islands", 
  10. "America/Los_Angeles"            => "(GMT-08:00) Pacific Time (US &amp; Canada)", 
  11. "America/Denver"                 => "(GMT-07:00) Mountain Time (US &amp; Canada)", 
  12. "America/Chihuahua"              => "(GMT-07:00) Chihuahua, La Paz, Mazatlan", 
  13. "America/Dawson_Creek"           => "(GMT-07:00) Arizona", 
  14. "America/Belize"                 => "(GMT-06:00) Saskatchewan, Central America", 
  15. "America/Cancun"                 => "(GMT-06:00) Guadalajara, Mexico City, Monterrey", 
  16. "Chile/EasterIsland"             => "(GMT-06:00) Easter Island", 
  17. "America/Chicago"                => "(GMT-06:00) Central Time (US &amp; Canada)", 
  18. "America/New_York"               => "(GMT-05:00) Eastern Time (US &amp; Canada)", 
  19. "America/Havana"                 => "(GMT-05:00) Cuba", 
  20. "America/Bogota"                 => "(GMT-05:00) Bogota, Lima, Quito, Rio Branco", 
  21. "America/Caracas"                => "(GMT-04:30) Caracas", 
  22. "America/Santiago"               => "(GMT-04:00) Santiago", 
  23. "America/La_Paz"                 => "(GMT-04:00) La Paz", 
  24. "Atlantic/Stanley"               => "(GMT-04:00) Faukland Islands", 
  25. "America/Campo_Grande"           => "(GMT-04:00) Brazil", 
  26. "America/Goose_Bay"              => "(GMT-04:00) Atlantic Time (Goose Bay)", 
  27. "America/Glace_Bay"              => "(GMT-04:00) Atlantic Time (Canada)", 
  28. "America/St_Johns"               => "(GMT-03:30) Newfoundland", 
  29. "America/Araguaina"              => "(GMT-03:00) UTC-3", 
  30. "America/Montevideo"             => "(GMT-03:00) Montevideo", 
  31. "America/Miquelon"               => "(GMT-03:00) Miquelon, St. Pierre", 
  32. "America/Godthab"                => "(GMT-03:00) Greenland", 
  33. "America/Argentina/Buenos_Aires" => "(GMT-03:00) Buenos Aires", 
  34. "America/Sao_Paulo"              => "(GMT-03:00) Brasilia", 
  35. "America/Noronha"                => "(GMT-02:00) Mid-Atlantic", 
  36. "Atlantic/Cape_Verde"            => "(GMT-01:00) Cape Verde Is", 
  37. "Atlantic/Azores"                => "(GMT-01:00) Azores", 
  38. "Europe/Belfast"                 => "(GMT) Greenwich Mean Time : Belfast", 
  39. "Europe/Dublin"                  => "(GMT) Greenwich Mean Time : Dublin", 
  40. "Europe/Lisbon"                  => "(GMT) Greenwich Mean Time : Lisbon", 
  41. "Europe/London"                  => "(GMT) Greenwich Mean Time : London", 
  42. "Africa/Abidjan"                 => "(GMT) Monrovia, Reykjavik", 
  43. "Europe/Amsterdam"               => "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna", 
  44. "Europe/Belgrade"                => "(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague", 
  45. "Europe/Brussels"                => "(GMT+01:00) Brussels, Copenhagen, Madrid, Paris", 
  46. "Africa/Algiers"                 => "(GMT+01:00) West Central Africa", 
  47. "Africa/Windhoek"                => "(GMT+01:00) Windhoek", 
  48. "Asia/Beirut"                    => "(GMT+02:00) Beirut", 
  49. "Africa/Cairo"                   => "(GMT+02:00) Cairo", 
  50. "Asia/Gaza"                      => "(GMT+02:00) Gaza", 
  51. "Africa/Blantyre"                => "(GMT+02:00) Harare, Pretoria", 
  52. "Asia/Jerusalem"                 => "(GMT+02:00) Jerusalem", 
  53. "Europe/Minsk"                   => "(GMT+02:00) Minsk", 
  54. "Asia/Damascus"                  => "(GMT+02:00) Syria", 
  55. "Europe/Moscow"                  => "(GMT+03:00) Moscow, St. Petersburg, Volgograd", 
  56. "Africa/Addis_Ababa"             => "(GMT+03:00) Nairobi", 
  57. "Asia/Tehran"                    => "(GMT+03:30) Tehran", 
  58. "Asia/Dubai"                     => "(GMT+04:00) Abu Dhabi, Muscat", 
  59. "Asia/Yerevan"                   => "(GMT+04:00) Yerevan", 
  60. "Asia/Kabul"                     => "(GMT+04:30) Kabul", 
  61. "Asia/Yekaterinburg"             => "(GMT+05:00) Ekaterinburg", 
  62. "Asia/Tashkent"                  => "(GMT+05:00) Tashkent", 
  63. "Asia/Kolkata"                   => "(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi", 
  64. "Asia/Katmandu"                  => "(GMT+05:45) Kathmandu", 
  65. "Asia/Dhaka"                     => "(GMT+06:00) Astana, Dhaka", 
  66. "Asia/Novosibirsk"               => "(GMT+06:00) Novosibirsk", 
  67. "Asia/Rangoon"                   => "(GMT+06:30) Yangon (Rangoon)", 
  68. "Asia/Bangkok"                   => "(GMT+07:00) Bangkok, Hanoi, Jakarta", 
  69. "Asia/Krasnoyarsk"               => "(GMT+07:00) Krasnoyarsk", 
  70. "Asia/Hong_Kong"                 => "(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi", 
  71. "Asia/Irkutsk"                   => "(GMT+08:00) Irkutsk, Ulaan Bataar", 
  72. "Australia/Perth"                => "(GMT+08:00) Perth", 
  73. "Australia/Eucla"                => "(GMT+08:45) Eucla", 
  74. "Asia/Tokyo"                     => "(GMT+09:00) Osaka, Sapporo, Tokyo", 
  75. "Asia/Seoul"                     => "(GMT+09:00) Seoul", 
  76. "Asia/Yakutsk"                   => "(GMT+09:00) Yakutsk", 
  77. "Australia/Adelaide"             => "(GMT+09:30) Adelaide", 
  78. "Australia/Darwin"               => "(GMT+09:30) Darwin", 
  79. "Australia/Brisbane"             => "(GMT+10:00) Brisbane", 
  80. "Australia/Hobart"               => "(GMT+10:00) Hobart", 
  81. "Asia/Vladivostok"               => "(GMT+10:00) Vladivostok", 
  82. "Australia/Lord_Howe"            => "(GMT+10:30) Lord Howe Island", 
  83. "Etc/GMT-11"                     => "(GMT+11:00) Solomon Is, New Caledonia", 
  84. "Asia/Magadan"                   => "(GMT+11:00) Magadan", 
  85. "Pacific/Norfolk"                => "(GMT+11:30) Norfolk Island", 
  86. "Asia/Anadyr"                    => "(GMT+12:00) Anadyr, Kamchatka", 
  87. "Pacific/Auckland"               => "(GMT+12:00) Auckland, Wellington", 
  88. "Etc/GMT-12"                     => "(GMT+12:00) Fiji, Kamchatka, Marshall Is", 
  89. "Pacific/Chatham"                => "(GMT+12:45) Chatham Islands", 
  90. "Pacific/Tongatapu"              => "(GMT+13:00) Nuku‘alofa", 
  91. "Pacific/Kiritimati"             => "(GMT+14:00) Kiritimati"
  92. ); 

2. 获取GMT标准时间

Php代码 技术分享

  1. echo ‘GMT :‘.date("m/d/Y H:i", time() - date("Z"));  

3. GMT to local time

下面的程序结合上面两条,将更加有用。

Java代码 技术分享

  1. <?php 
  2. // I am using the convention (assumption) of "07/04/2004 14:45"
  3. $processdate = "12/04/2010 14:45"; //  date("m/d/Y H:i", time() - date("Z")) is GMT
  4. // gmttolocal is a function
  5. // i am passing it 2 parameters:
  6. // 1)the date in the above format and
  7. // 2)time difference as a number; -5 in our case (GMT to CDT)
  8. echo gmttolocal($processdate,-5);  
  9. function gmttolocal($mydate,$mydifference)   
  10. // trying to seperate date and time
  11.     $datetime = explode(" ",$mydate); 
  12. // trying to seperate different elements in a date
  13.     $dateexplode = explode("/",$datetime[0]); 
  14. // trying to seperate different elements in time
  15.     $timeexplode = explode(":",$datetime[1]); 
  16. // getting the unix datetime stamp
  17.     $unixdatetime = mktime($timeexplode[0]+$mydifference,$timeexplode[1],0,$dateexplode[0],$dateexplode[1],$dateexplode[2]); 
  18. // return the local date
  19. 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获得可靠的精准的当前时间 ( 通过授时服务器 )

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。