Home >Backend Development >PHP Problem >How to modify the time difference in php

How to modify the time difference in php

藏色散人
藏色散人Original
2022-11-19 09:17:071086browse

php solution to modify the time difference: 1. Eliminate the time difference through "date_default_timezone_set('Asia/Shanghai');"; 2. Through "gmdate("Y-m-d H:i:s", mktime() $timeoffset * 3600);"Formatting time.

How to modify the time difference in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php How to modify the time difference?

  • PHP method to get the current time

Use function date() to implement

<?php
date_default_timezone_set(&#39;Asia/Shanghai&#39;);//此句用于消除时间差
echo $nowtime=date("Y-n-j H:i:s");
?>
  • The solution to the time difference

You can use the following code:

<?php
$timeoffset = 8;
echo gmdate("Y-m-d H:i:s", mktime() + $timeoffset * 3600);
//gmdate()函数返回的是GMT(格林威治)时间,$timeoffset是各地的时差。8是北京时间和GMT的时差。
?>

Also~ Starting from php5.1.0, php.ini The date.timezone option has been added, which is turned off by default

That is, the time displayed (no matter what php command is used) is Greenwich Mean Time

and our time (Beijing time) is exactly 8 hours behind. There are three methods below to restore the normal time.

1, the simplest way is not to use php5.1 or above version

2, if you must use it, and you cannot modify php.ini, you need to initialize the time statement Add date_default_timezone_set (XXX);

above.

3, once and for all, only php.ini can be modified. Open php.ini and search for date.timezone. Remove the semicolon in front

=, add PRC after it, and restart the http service (such as apache2 or iis, etc.).

Regarding the XXX in the brackets of 2, you can Go to PHP official website to check, address: http://www.php.net/manual/en/timezones.php

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to modify the time difference in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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