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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles