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?
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>
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('PRC'); echo date("Y-m-d H:i:s"); ?>
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!