简体   繁体   English

PHP date() 为不同的用户返回不同的值

[英]PHP date() returning different values for different users

I have a problem with some users receiving different dates in my project.我遇到了一些用户在我的项目中收到不同日期的问题。 The project runs under PHP 7.4 on IIS.该项目在 IIS 上的 PHP 7.4 下运行。

My Machine is in Vancouver and the users are in Brisbane Australia我的机器在温哥华,用户在澳大利亚布里斯班

I've isolated the issue to the code below:我已将问题隔离到以下代码:

$tzone = 'Australia/Brisbane';
$html  = '<br>Machine Date: '. date("Y-m-d H:i:s");
$html .= '<br>TimeZone: '. $tzone;
$tnow = new DateTime("now", new DateTimeZone($tzone));
$html .= '<br>Local Time: '.  $tnow->format('Y-m-d H:i:s') ;
echo $html;

Most users (over 100) receive the following (which is expected):大多数用户(超过 100 名)收到以下内容(这是预期的):

Machine Date: 2021-12-06 16:00:34
TimeZone: Australia/Brisbane
Local Time: 2021-12-07 10:00:34

However 2 get:然而 2 得到:

Machine Date: 2021-12-07 11:00:34
TimeZone: Australia/Brisbane
Local Time: 2021-12-07 10:00:34

I'm guessing this is something to do with the configuration of these users but havent been able to discover what it is.我猜这与这些用户的配置有关,但无法发现它是什么。 The users are set to their local timezones correctly and their clocks are set to the right time.用户被正确设置为他们的本地时区,他们的时钟被设置为正确的时间。

IF YOU'RE DEVELOPING ON LOCALHOST如果您正在本地主机上开发

There's Nothing Wrong With The Codes.代码没有错。 The MACHINE DATE Displays the Users Computer Time and has nothing to do with the zone you specified. MACHINE DATE显示用户计算机时间,与您指定的区域无关。

The Local Time is affected by the zone you specified.本地时间受您指定的时区影响。

Machine Date: = Users Local Computers' Time (Not Affected By the zone you specified)机器日期: = 用户本地计算机的时间(不受您指定的区域的影响)

TimeZone: You need to specify what timezone should affect the last $html (Local Time) TimeZone:您需要指定哪个时区应该影响最后一个 $html(本地时间)

Local Time: is influenced by the time zone you specified.本地时间:受您指定的时区影响。

LIVE SERVER实时服务器

On a live Server, the timezone is given by the server which you use.在实时服务器上,时区由您使用的服务器提供。

Use PHP TimeZone使用PHP 时区

<?php

date_default_timezone_set('Africa/Lagos'); // set the default timezone here to use for machine date

$tzone = 'Australia/Brisbane';
$html  = '<br>Machine Date: '. date("Y-m-d H:i:s");
$html .= '<br>TimeZone: '. $tzone;
$tnow = new DateTime("now", new DateTimeZone($tzone));
$html .= '<br>Local Time: '.  $tnow->format('Y-m-d H:i:s') ;
echo $html;

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM