How to set the time zone in php

Source: Internet
Author: User
Tags current time echo date local time

In php. ini, the default value is date. timezone = UTC. Change to China time zone and date. timezone = PRC. If the GMT format is directly written, it is date. timezone = Etc/GMT + 8.

You can also set it in the PHP page header.

The code is as follows: Copy code

Date_default_timezone_set ('prc ');


In PHP5, there are many ways to set or obtain the default time zone settings. For example, you can use the date_default_timezone_setl function to set the time zone.

The code is as follows: Copy code

<? Php
Date_default_timezone_set ("Asia/Shanghain"); // Set the time zone to Shanghai.
?>

Or set the Tokyo time zone code:

<? Php
Date_default_timezone_set ("Asia/Tokyo ");
?>

When the system is initialized, add

Ini_set ('date. timezone ', 'Asia/Shanghai ');

Or

Date_default_timezone_set ("PRC ");

The time zone difference is 8.


If you have php. ini management permissions, you can directly modify them in php. ini.


Manually modify php. ini settings

Open php and find date. timezone = "PRC". If the semicolon is removed, add it manually!

 

After installing PHP5, you will find the following problems:

 

The code is as follows: Copy code
<? Php
$ Atime = date ("Y-m-d H: I: s ");
Echo $ atime;
?>
<? Php
$ Atime = date ("Y-m-d H: I: s ");
Echo $ atime;
?>

You may find that the output time is different from the current time.

The reason is that if you do not set the local time zone of your server in the program or configuration file, the time taken by PHP is the Greenwich mean time, so it may be different from your local time.

The GMT standard time is about eight hours behind Beijing time. How can we avoid time errors?

Let's take a look at the solution:

Use date_default_timezone_set () in the header to set my default time zone to Beijing time.

The code is as follows: Copy code


<?
Date_default_timezone_set ('prc ');
Echo date ('Y-m-d H: I: s ');
?>


The time is the same as the current time of the server.

If a database insertion error occurs, make sure that H in date ('Y-m-d H: I: s') is in uppercase.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.