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获得可靠的精准的当前时间 ( 通过授时服务器 )

时间: 2024-11-05 18:35:39

php获取当地时间 time zone的相关文章

获取当地时间

NSDate *date = [NSDate date]; 方法一 NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: date]; NSDate *localeDate = [date  dateByAddingTimeInterval: interval]; NSLog(@"%@", localeDate); 方法二 NSDateForma

php大力力 [055节] 效率低啊,效率低

php大力力 [055节] 效率低啊,效率低 啥也不说了,先把网页挨个保存一下,关闭网页窗口 从 2015-09-11 10:58 到 2015-09-11 12:08 共用了70分钟,整理最近几天看过的,没有关闭的文章如下: 俞敏洪:你以为寒冬很远寒冬只是一夜之间 王思聪都"心动"的公司:融资2.5亿,打造“养成系”偶像女团 用PHP获取当前时间 - 开源中国社区 php获取当地时间 time zone - Just Code - ITeye技术网站 <input type=&

C语言 时间函数的学习和总结

一直都是以简单的time_t t,time(&t),ctime(&t)来表示时间,后来要以时间为日志文件的名字时,就有点蒙逼了.学习一下. tm结构: struct tm { int tm_sec; /* 秒 – 取值区间为[0,59] */ int tm_min; /* 分 - 取值区间为[0,59] */ int tm_hour; /* 时 - 取值区间为[0,23] */ int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ int tm_mon; /*

10.时间编程

1.时间的类型: Coordinated Universal Time(UTC):世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT). Calendar Time:日历时间,是用"从一个标准时间点(如:1970年1月1日0点)到此时经过的秒数"来表示的时间. ? 2.时间操作: 获取日历时间:man 2 time: (主要:man 后面的数字表示的是章节:1(留空):命令 2:函数 3:系统调用.) NAME time - get tim

Centos7的firewalld配置

红帽官方的使用文档: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html 常用命令 # 查看版本 [[email protected] java]# firewall-cmd --version 0.3.9 # 查看状态 [[email protected] java]# systemctl status fire

两个月的php学习我学了些什么

在lovo学习php已经两个月了,这两个月自己又学会了些什么呢? 什么是php?这是刚刚开始的疑问,开始只是很模糊的理解成web服务器的后台应用程序什么的,到后来学习后才慢慢的认识了php,php的全名是HyperText Preprocessor(超文本预处理器),最开始发明者的目的是用来处理自己的个人主页的,因此php最开始是代表Personal Home Page,发展至今,php不在是简单的处理服务器端的脚本程序了(例如:从表单中收集资料:产生动态的网页内容:传送及接收cookies等)

NSDate(十六)

NSDate是oc的一个日期和时间的处理类 当拿到一个新的类的时候,首先想到的是这个类的初始化方法,这个NSDate类的初始化方法是: NSDate *date = [[NSDate alloc]init];//格林威治时间 NSLog(@"%@",date);//2015-04-2109:54:53.564 OCLesson7_NSDate[787:27248] 2015-04-21 01:54:53 +0000//格林威治时间 NSDate类的初始化方法,得到的值是格林威治时间,因

C#/VB.NET语法的比较 转

最近因为工作的关系,需要从用了三年的C#转用VB.NET啦.没有办法,本来嘛,使用语言是没有很大的关系的.但刚刚使用的时候,多多少少还是有一些磕磕碰碰的.主要是代码编写的操作便捷方面,总结几点: ◎代码编定实现Interface时 C#中使用 :Interface,然后按shift+alt+F10回车,呵呵,全部接口需实现的方法属性全部自动生成.当然这个是2005才有的.2003不知有没有,反正我是没有找着.况且2005也算是普遍了.VB中使用 Implements,光标定位至相应的Interf

时间与时间戳的转换

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 时间戳<span>是一种时间表示方式,定义为从</span><a href="http://baike.baidu.com/view/856.htm" target="_blank">格林威治时间</a><span