Home > Backend Development > PHP Problem > Why are the times obtained by php different?

Why are the times obtained by php different?

angryTom
Release: 2023-02-28 18:16:01
Original
2649 people have browsed it

Getting time in php is a very common operation. Sometimes when we test the program, we will find that the time obtained is different. What is the reason? How to solve it? Let's take a look at the solution to the different times obtained by php.

Why are the times obtained by php different?

#Why are the times obtained by php different?

Use PHP to get the time and find that the time is wrong. This is because the default time zone of PHP is UTC, Universal Time Coordinated (UTC). The solution is to set the time to Beijing time.

The specific operation method is as follows:

Method 1: Modify the php.ini file

(1) Open php.ini File

(2) Search for date.timezone and find date.timezone="UTC",

(3) Change it to date.timezone = "PRC", if there is a semicolon on the left side of date.timezone, remove the semicolon.

(4) Test in the php program, enter the following code

<span class="cur_time"><?php echo "当前时间:" . date("Y-m-d H:i:s") ?></span>
Copy after login

Method 2: Use the date_default_timezone_set('') method

(1) Add the following code at the beginning of the php program

<?php
date_default_timezone_set(&#39;PRC&#39;); 
echo date("Y-m-d H:i:s");
?>
Copy after login

Recommended related articles and tutorials: php tutorial

The above is the detailed content of Why are the times obtained by php different?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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