PHP中设置时区方法小结_php技巧
找到原因后,在网上搜索到了一些关于PHP的时区设置方法:
1、修改php.ini,在php.ini中找到data.timezone =去掉它前面的;号,然后设置data.timezone = “Asia/Shanghai”;即可。
2、在程序PHP 5以上版本的程序代码中使用函数ini_set('date.timezone','Asia/Shanghai');或者date_default_timezone_set(‘Asia/Shanghai');
一些常用的时区标识符说明:
Asia/Shanghai – 上海
Asia/Chongqing – 重庆
Asia/Urumqi – 乌鲁木齐
Asia/Hong_Kong – 香港
Asia/Macao – 澳门
Asia/Taipei – 台北
Asia/Singapore – 新加坡
函数设置时区方法:
function_exists(date_default_timezone_set);//在这他总是返回1,这函数是判断这里面的字符是不是一个定义了的函数名
date_default_timezone_set("Etc/GMT");//这是格林威治标准时间,得到的时间和默认时区是一样的
date_default_timezone_set("Etc/GMT+8");//这里比林威治标准时间慢8小时
date_default_timezone_set("Etc/GMT-8");//这里比林威治标准时间快8小时
date_default_timezone_set('PRC'); //设置中国时区
?>
函数ini_set()设置时区:
可以在文件开头加入 ini_set('date.timezone','Asia/Shanghai'); // 'Asia/Shanghai' 为上海时区
手动修改php.ini设置
打开php找到date.timezone = "PRC" 如有去掉前面的分号,没有的话手动添加!
以下是一些资料补充:
装上PHP5后你会发现这样的问题
$atime=date("Y-m-d H:i:s");
echo $atime;
?>
输出:2006-05-16 06:36:06
现在几点了?/我的是14:36
这是为什么捏?
原因是假如你不在程序或配置文件中设置你的服务器当地时区的话
PHP所取的时间是格林威治标准时间,所以和你当地的时间会有出入
格林威治标准时间和北京时间大概差8个小时左右 那么我们如何避免时间误差呢?
我们一起来看看解决方法:
在页头使用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 级别的错误信息。
参数
timezone_identifier
时区标识符,例如 UTC 或 Europe/Lisbon
返回值
本函数永远返回 TRUE(即使 timezone_identifier 参数不合法)。
-------------------------------------------------------------------------------------
或者修改 php.ini 中 date.timezone 值
date.timezone = PRC
装上PHP5后你会发现这样的问题
$atime=date("Y-m-d H:i:s");
echo $atime;
?>
输出:2006-05-16 06:36:06
现在几点了?/我的是14:36
这是为什么捏?
原因是假如你不在程序或配置文件中设置你的服务器当地时区的话
PHP所取的时间是格林威治标准时间,所以和你当地的时间会有出入
格林威治标准时间和北京时间大概差8个小时左右 那么我们如何避免时间误差呢?
我们一起来看看解决方法:
在页头使用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 级别的错误信息。
参数
timezone_identifier
时区标识符,例如 UTC 或 Europe/Lisbon
返回值
本函数永远返回 TRUE(即使 timezone_identifier 参数不合法)。
-------------------------------------------------------------------------------------
或者修改 php.ini 中 date.timezone 值
date.timezone = PRC

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



A time zone refers to the standard time used in a region on Earth. There are 24 time zones in the world, from UTC-12 to UTC+14. When traveling between countries with different time zones, you need to adjust your time according to each country's time zone. PHP has some built-in time functions that allow the time in any time zone around the world to be used.

Title: Use the time.Unix function to convert Unix timestamps to time format and set the time zone. In the Go language, we often need to convert Unix timestamps into a readable time format, and often need to consider setting the time zone. This article will introduce how to use the Unix function in the time package to accomplish this task, and demonstrate how to set the time zone. First, we need to understand what Unix timestamp means. A Unix timestamp is an integer in seconds representing the time since January 1, 1970

Friends, do you know how to select the time zone when booking a Tencent meeting? Today, the editor will explain how to select the time zone when booking a Tencent meeting. If you are interested, come and take a look with the editor. I hope it can help you. Step one: Enter the Tencent Meeting APP and click to book a meeting (as shown in the picture). Step 2: Select the meeting type, such as regular meeting, and click Next (as shown in the picture). Step 3: On the meeting reservation page, click on the time zone (as shown in the picture). Step 4: Select the time zone (as shown in the picture). Step 5: If the setting is successful, click Finish (as shown in the picture).

Solution to changing the time zone in Linux does not take effect: 1. Log in to the Linux system and enter the terminal; 2. Copy the configuration file to modify the time zone; 3. View the hardware clock through the "hwclock --show" command, and then set the hardware clock; 4. Pass "vim /etc/timezone Asia/Shanghai" can modify the time zone.

How to modify the time zone in PHP: 1. Set the default time zone to East 8 in the configuration file; 2. Set the time zone through the code "date_default_timezone_set('Asia/Shanghai');"; 3. Set "date_default_timezone_set(' in the PHP page header PRC');".

Computers are a good helper in our lives and work. After using a computer for a period of time, the system will freeze. At this point, you need to reinstall the system to solve the problem. So how to install the system on a desktop computer? Let me share with you the steps on how to install the system on your computer. 1. After downloading the system format ISO file, copy the ISO file and NT6 installer (which can be downloaded by yourself) to the root directory of a drive other than the C drive on your computer. The two files must be in the same directory. 2. Open the NT6 installer. After opening, the following picture will appear. Select mode 2 for installation. 3. After restarting, enter the image below. Press the image below in the startup selection menu to select entry. 4. Click Next to enter. For a new installation, click the "Custom (Advanced)" option. 5. After clicking Customize Advanced, the

PHP is one of the most popular server scripting languages used for web development, especially dynamic web development. With the popularity of mobile Internet, timestamps have become more and more common in web development, so converting timestamps into date and time in a specific format in PHP is a very important skill. This article will introduce how to convert timestamps in PHP, as well as time zone settings.

PHP is a very popular server-side programming language. When developing web applications using PHP, we sometimes need to set the time zone in PHP. The default time zone of PHP is "UTC (Coordinated Universal Time)", which is not the time zone we want in many cases, so we need to change the time zone setting in the php.ini file. This article will explain how to change the time zone in the php.ini file.
