PHP time zone setting problem_PHP tutorial

WBOY
Release: 2016-07-13 10:32:56
Original
943 people have browsed it

After installing PHP5 you will find this problem:

<?php
$atime=date("Y-m-d H:i:s");
echo $atime;
?>
Copy after login

You may find that the output time is different from your 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 Greenwich Mean Time, so it will be different from your local time.

The difference between Greenwich Mean Time and Beijing Time is about 8 hours. So 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.

<?
date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s');
?>
Copy after login

The time is the same as the current server time.

If an insertion database error occurs, please make sure the H in date('Y-m-d H:i:s') is uppercase.

In addition, the usage of date_default_timezone_set is as follows:

date_default_timezone_set

(PHP 5 >= 5.1.0RC1)

date_default_timezone_set -- Set the default time zone used for all date and time functions in a script

Description

bool date_default_timezone_set ( string timezone_identifier )

date_default_timezone_set() sets the default time zone used for all datetime functions.

Note: Since PHP 5.1.0 (the date and time functions have been rewritten in this version), if the time zone is invalid, each call to the date and time function will generate an E_NOTICE level error message.

Parameters

timezone_identifier

Time zone identifier, such as UTC or Europe/Lisbon

Return value

This function always returns TRUE (even if the timezone_identifier parameter is illegal).

Or modify the date.timezone value in php.ini, date.timezone = PRC

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752579.htmlTechArticleAfter installing PHP5, you will find this problem: ?php$atime=date("Y-m-d H:i: s");echo $atime;?> You may find that the output time is different from your current time. The reason is if you...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template