Home Backend Development PHP Tutorial PHP的php.ini时区设置问题

PHP的php.ini时区设置问题

Jun 23, 2016 pm 02:29 PM

PHP的php.ini时区设置问题

2009-09-10 15:40

从php5.1.0开始,php.ini里加入了date.timezone这个选项,默认情况下是关闭的,也就是显示的时间(无论用什么php命令)都是格林威治标准时间,和我们的时间(北京时间)差了正好8个小时,有以下3中方法可以恢复正常的时间。
1、最简单的方法就是不要用php5.1以上的版本;
2、如果要用5.1以上版本,而且不修改php.ini,则需要在关于时间的初始化的语句的上面加上date_default_timezone_set (XXX),或者使用date('Y-m-d G:i:T', strtotime('+8HOUR') )来获取日期时间;
3,一劳永逸,仅限能修改php.ini。打开php.ini把date.timezone前面的分号去掉,在=后面加XXX,重启http服务(如apache2或iis等)即可。
关于XXX,大陆内地可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi(依次为重庆,上海,乌鲁木齐),港台地区可用:Asia/Maca* ,Asia/Hong_Kong ,Asia/Taipei(依次为澳门,香港,台北),还有新加坡:Asia/Singapore,以上没有北京,不过接着往下看,其他可用的值是:Etc/GMT-8,Singapore ,Hongkong,PRC。PRC是什么?PRC是中华人民共和国啊!(这个就是北京时间吧)以上都是php官方说明档里整理出来的GMT-8下面的地区,可能会有遗漏,如有需要再上官方文档里查看一下比较好:)

今天在PHP5下用date("H:i:s")时,发现参数"H"取出的时间与window下的时间不对。查了一下资料,发现是PHP5的php.ini里面默认设置为:

[Date]
; Defines the default timezone used by the date functions
;date.timezone =

如此一来,按照默认的时间便为GMT时间。而我们一般是使用北京时间,可以设置为:date.timezone = PRC或date.timezone = Asia/Shanghai。即:

[Date]
; Defines the default timezone used by the date functions
date.timezone = Asia/Shanghai

记得不要设置为"Asia/Beijing",老外好象对上海感兴趣点,呵呵。

如果没有权限改php.ini,可以用函数date_default_timezone_set('PRC');或date_default_timezone_set('Asia/Shanghai');

这个函数用于设定所有日期时间函数的默认时区。手册上如此说明:“自 PHP 5.1.0 起(此版本日期时间函数被重写了),如果时区不合法则每个对日期时间函数的调用都会产生一条 E_NOTICE 级别的错误信息”。但是“本函数永远返回 TRUE(即使

在此再学习一下函数:string

这个函数的返回值遵循以下顺序:1:用 TZ 环境变量(如果非空)。3:date.timezone 配置选项(如果设定了的话)。4:自己推测(如果操作系统支持)。5:如果以上选择都不成功,则返回

再深入学习一下什么是UTC:
协调世界时(UTC): 
一种称为协调世界时的折衷时标于1972年面世。为了确保协调世界时与世界时(UT1)相差不会超过0.9秒,有需要时便会在协调世界时内加上正或负闰秒。因此协调世界时与国际原子时(TAI)之间会出现若干整数秒的差别。位于巴黎的国际地球自转事务中央局(IERS)负责决定何时加入闰秒。

UTC = Coordinated Universal Time. 中文名称为协调世界时.

GMT = Greenwich Mean Time. 中文名称为格林尼治(平)时(这里的"w"是不发音的,而且"Green"要读成"Gren")

UTC = GMT +/- 0.9 s 
因此 UTC 间中需要进行 "闰秒" 以控制两者相差。

 

php5.1x的时区问题导致相差八个小时!收藏
从php5.10开始,php中加入了时区的设置,在php中显示的时间都是格林威治标准时间,这就造成了我们中国的用户会差八个小时的问题!
相关设置是修改php.ini中的 date.timezone 参数:
[Date]
; Defines the default timezone used by the date functions
;date.timezone =

默认是关闭的,只需把注释去掉,改为即可
[Date]
; Defines the default timezone used by the date functions
date.timezone = PRC

其中PRC是“中华人民共和国”!
其他选项可以参考php手册。
不过这上面的亚洲地区漏掉了我们的首都北京,不知道老外是不是故意的!

如果没有修改php.ini的权限,只需要在调用时间日期函数的时候,调用 date_default_timezone_set(’PRC’) 即可!
也可以调用date_default_timezone_get()来查看当前的时区设置!

?於XXX,大??地可用的值是:
Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次?重?,上海,??木?)
港台地?可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次?澳?,香港,台北)
台?地区可??:date.timezone = "Asia//Taipei"
?有新加坡:Asia/Singapore

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/keenx/archive/2006/03/21/631432.aspx

摘自dedecms :php5 时区设置
if(PHP_VERSION > '5.1') {
$time51 = 'Etc/GMT'.($cfg_cli_time > 0 ? '-' : '+').abs($cfg_cli_time);
function_exists('date_default_timezone_set') ? @date_default_timezone_set($time51) : '';
}

如果php版本大于5.1执行
$cfg_cli_time = -8;意思就是少8个小时!格式化后用data_default_timezone_set("Etc/GMT+8")设置!!
北京时区应该是Etc/GMT+8

时区设置 一般都设置成+8个小时!
是格林威治标准时(GMT)
$date = gmdate("Y-m-d H:i:s",time()+8*3600)
只要满足加8个小时就可以!写成函数或者什么..就随便了

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles