Home Backend Development PHP Tutorial PHP中遇到的时区问题解决方法_php技巧

PHP中遇到的时区问题解决方法_php技巧

May 16, 2016 pm 08:10 PM
php Time zone issue

最近在学习PHP过程中发现PHP中的格式化时间戳比北京时间晚了8个小时,上网搜索发现原来是时区不对,解决办法是:

     1、永久修改

          更改php.ini文件中的data.timezone = PRC  这是中国时间。重启Apache服务后生效。

     2、临时修改

          在使用格式化输入时间之前,添加ini_set('date.timezone','Asia/Shanghai');    
          或者data_default_timezone_set('PRC');//date_default_timezone_set() 函数设置用在脚本中所有日期/时间函数的默认时区。

看看其他小伙伴遇到的问题

  之前有一个遗留问题,就是echo date("Y-m-d H:i:s",time())回来的时间总是与实际时间对不上,今天终于在网上找到了原因和解决方法,分享如下:

补充一点,按照下面提示的方法修改php.ini找不到date.timezone 这行,是不是就没办法了呢,当然不是啦,哈哈没有的话就自己加嘛,自己动手丰衣足食。加了这样一句date.timezone = "PRC",问题就搞定了,高兴

从php5.1.0开始,php.ini里加入了date.timezone这个选项,默认情况下是关闭的

也就是显示的时间(无论用什么php命令)都是格林威治标准时间,和我们的时间(北京时间)差了正好8个小时,有以下3中方法可以恢复正常的时间。

1,最简单的方法就是不要用php5.1以上的版本

2,如果一定要用,而且不能修改php.ini,则需要在关于时间的初始化的语句的上面加上 date_default_timezone_set (XXX);

3,一劳永逸,仅限能修改php.ini。打开php.ini查找date.timezone 去掉前面的分号
= 后面加XXX,重启http服务(如apache2或iis等)即可

关于XXX,大陆内地可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次为重庆,上海,乌鲁木齐)

港台地区可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次为澳门,香港,台北)

还有新加坡:Asia/Singapore

老外好像把北京漏调了

其他可用的值是:Etc/GMT-8 ,Singapore ,Hongkong ,PRC

PRC是什么?PRC是中华人民共和国啊-_-

———————————————————————————————————————

解决方式: 在页头使用date_default_timezone_set()设置我的默认时区为北京时间 date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s'); 时间和服务器当前时间一样了!!祝贺之 附date_default_timezone_set用法如下

——————————–

date_default_timezone_set
(PHP 5 >= 5.1.0RC1)
date_default_timezone_set — 设定用于一个脚本中所有日期时间函数的默认时区说明
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() 设定用于所有日期时间函数的默认时区。
注: 自 PHP 5.1.0 起(此版本日期时间函数被重写了),如果时区不合法则每个对日期时间函数的调用都会产生一条 E_NOTICE 级别的错误信息。

以上所述上就是本文的全部内容了,希望大家能够喜欢。

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
4 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles