`
天梯梦
  • 浏览: 13747020 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

php获取当地时间 time zone

阅读更多

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

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

date.timezone = Asia/Shanghai
 

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

 

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

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

date_default_timezone_set('Asia/Shanghai');
ini_set('date.timezone','Asia/Shanghai');
 

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

 

 

 

<?php
include('tz.php');

echo get_localtime(time(),'Asia/Hong_Kong'); // local time

//get_alltime(time()); // global time

?>
 

具体见附件;

 

 

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

 

1. PHP TimeZone Array

 

<?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 &amp; Canada)",
	"-7" => "(GMT -7:00) Mountain Time (US &amp; Canada)",
	"-6" => "(GMT -6:00) Central Time (US &amp; Canada), Mexico City",
	"-5" => "(GMT -5:00) Eastern Time (US &amp; 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"
);
 

 

    $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'
    );
 
        $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 &amp; Canada)",
            "America/Denver"                 => "(GMT-07:00) Mountain Time (US &amp; 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 &amp; Canada)",
            "America/New_York"               => "(GMT-05:00) Eastern Time (US &amp; 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标准时间

 

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

 

3. GMT to local time

 

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

 

 

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

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    IANA Time Zone Database (简称tzdata) 时区数据

    IANA Time Zone Database,通常被称为tzdata,是全球时间区数据的一个重要资源,它包含了世界各地的时区信息,包括夏令时规则、历史时区变更等。这个数据库被广泛用于操作系统、编程语言、网络服务和其他需要处理...

    世界时间对照TimeZone

    "世界时间对照TimeZone"这个主题涉及到的是如何处理和显示不同地理位置的时间。这通常与编程语言中的“时区”概念紧密相关,时区是根据地球上的经度划分的区域,每个区域都有自己的本地时间。在计算机系统中,我们...

    php报时区错误,按照网上说的改时间date.timezone根本不管用!怎么解决,看我的.zip

    php报时区错误,按照网上说的改时间date.timezone根本不管用!怎么解决,看我的.zip Warning: phpinfo() [function.phpinfo]: It is not safe to rely on the system's timezone settings. You are *required* to use ...

    Oracle Timestamp with Time zone & java

    在从数据库查询结果获取Timestamp with Time Zone数据时,可以通过`ResultSet.getObject()`方法,然后转换为`ZonedDateTime`。 在实际开发中,有时会遇到时区转换的问题。Oracle数据库中存储的是时区信息,而Java中...

    java.util.TimeZone 的世界时区列表

    - **获取时区**:可通过`TimeZone.getTimeZone("时区ID")`方法获取指定时区。 - **默认时区**:`TimeZone.getDefault()`返回系统默认时区。 - **设置时区**:`TimeZone.setDefault(TimeZone)`可以设置当前程序的...

    timezone_2021a_posix_sql

    【标题】"timezone_2021a_posix_sql"是一个与MySQL数据库系统相关的插件,主要用于处理时间区域设置。这个插件旨在解决在集成Django框架与Celery任务队列时可能出现的时间区域相关的问题。在分布式系统中,正确处理...

    js 根据本地时区获取 当地时间

    例如,以下代码使用`moment-timezone`库获取纽约的当地时间: ```javascript let moment = require('moment-timezone'); let now = moment().tz('America/New_York').format(); ``` 这里,`tz`方法接受一个时区...

    前端项目-moment-timezone.zip

    在这个前端项目中,开发者可以学习如何集成`moment-timezone`到自己的应用程序中,理解如何处理跨时区的时间显示问题,以及如何根据用户设备的时区调整时间显示。对于需要为全球用户提供服务的Web应用来说,这样的...

    TimeZone例子

    TimeZone例子TimeZone例子TimeZone例子TimeZone例子

    Delphi 无控件实现TimeZone世界时间对照程序

    在本文中,我们将深入探讨如何使用Delphi编程语言无控件地实现一个TimeZone世界时间对照程序。这个程序的目的是在不依赖任何特定图形用户界面组件的情况下,帮助用户查看选定时间在全球主要城市的时间对应情况,这...

    如何修改时区(TIMEZONE)

    如何修改时区(TIMEZONE)如何修改时区(TIMEZONE)如何修改时区(TIMEZONE)如何修改时区(TIMEZONE)

    django TIME_ZONE.7z

    Django内置了`django.utils.timezone`模块,其中包含了一些帮助函数,如`timezone.localtime()`用于将UTC时间转换为当前`TIME_ZONE`的时间。 六、最佳实践 1. 在生产环境中,确保`USE_TZ`设为True,以便利用UTC进行...

    前端项目-timezone-js.zip

    **前端项目-timezone-js** 是一个基于Olson时区数据的JavaScript库,专门用于处理和显示全球不同时区的时间。这个库为Web开发者提供了一种简单有效的方式来处理时区转换和时间显示,尤其是在构建需要跨时区交互的Web...

    TimeZone_demo_时区例子

    综上所述,`TimeZone_demo_时区例子`项目是一个实用的教学示例,它展示了如何在ASP.NET环境中处理时区选择,并利用C#的`.NET Framework`功能实现时区信息的获取和时间转换。这个项目对于开发面向全球用户的Web应用...

    timezone-data:一个Elm软件包,其中包含IANA时区数据库中的时区数据

    TimeZone.getZone任务获取客户端的本地Time.Zone及其区域名称。 import Time import TimeZone getZone : Task TimeZone . Error ( String , Time . Zone ) getZone = TimeZone . getZone 有关完整示例,请参。 ...

    时钟和日历里的Home time zone显示不一致

    今天说一下时钟和日历里的Home time zone显示不一致的问题,大家一起学习下。

    最新Google日历的时区选项 Time zone selector(中文+英文).txt

    最新Google日历的时区选择列表Time zone,2021年9月2日

    cpp-NTPClientESP8266的NTP客户端库带时区timezone支持

    标题中的"cpp-NTPClientESP8266的NTP客户端库带时区timezone支持"指的是一款专为ESP8266微控制器设计的C++实现的NTP(Network Time Protocol)客户端库,该库扩展了对时区timezone的支持。在物联网应用中,精确的...

    Laravel开发-timezone

    在Laravel框架中,`timezone`是一个非常关键的设置,它影响着整个应用程序中的日期和时间处理。`timezone`配置允许开发者指定应用默认时区,确保时间显示与用户所在的地理位置相匹配,提供更好的用户体验。本篇文章...

Global site tag (gtag.js) - Google Analytics