简体   繁体   English

PHP,服务器发送事件 - date_default_timezone_get()在远程文件中无法正常工作

[英]PHP, Server-Sent Events - date_default_timezone_get() does not work properly in remote file

I am using server-sent events, and trying to display times according to the local time of the viewer. 我正在使用服务器发送的事件,并尝试根据查看器的本地时间显示时间。

All times in my databases are stored as GMT, in DATETIME fields: 我的数据库中的所有时间都存储为GMT,位于DATETIME字段中:

Code Block 1 代码块1

date_default_timezone_set('GMT');
$now = date('Y-m-d H:i:s');

$stmt = $db->prepare('INSERT INTO...');
$stmt->execute(array($now));

And then the reverse (probably more complicated than it needs to be): 然后反过来(可能比它需要的更复杂):

Code Block 2 代码块2

$utimezone = date_default_timezone_get();
$then = date($row['ndate']);
$date = date_create($then);
$date->setTimezone(new DateTimeZone($utimezone));
$nDate = date_format($date, 'd M \'y (g:i A)');

This all works properly, when the window is live in the browser, but falls apart when date_default_timezone_get() is in a remotely-called file: 当窗口在浏览器中处于活动状态时,这一切都正常工作,但当date_default_timezone_get()位于远程调用的文件中时,这一切都会崩溃:

JavaScript JavaScript的

Code Block 3 代码块3

var src = new EventSource('sse_bot.php?uid=' + userID);

where the date_default_timezone_get() is in the sse_bot.php file. 其中date_default_timezone_get()位于sse_bot.php文件中。

I tried setting a session variable from the browser window: 我尝试从浏览器窗口设置会话变量:

Code Block 4 代码块4

$utimezone = date_default_timezone_get();
$_SESSION['tzone'] = $utimezone;

and then using it in the sse_bot.php file: 然后在sse_bot.php文件中使用它:

Code Block 5 代码块5

$utimezone = $_SESSION['tzone'];

But no dice. 但没有骰子。

Any thoughts? 有什么想法吗?

EDIT 1 编辑1

I echoed it back, and $utimezone = $_SESSION['tzone']; 我回复了它,并且$utimezone = $_SESSION['tzone']; does set it to the appropriate value ('America/Denver' in my case). 设置为适当的值(“美国/丹佛”在我的情况)。 But it seems that the issue is, specifically, with the line: 但似乎问题是,具体来说,问题是:

Code Block 6 代码块6

$date->setTimezone(new DateTimeZone($utimezone));

When I use Code Block 2 through a browser, it works perfectly. 当我通过浏览器使用Code Block 2时 ,它运行得很好。 I can even change the line to: 我甚至可以将线路更改为:

Code Block 7 代码块7

$date->setTimezone(new DateTimeZone('America/Denver'));

And all is well. 一切都很好。

Making the identical change to the sse_bot.php file has no effect. sse_bot.php文件进行相同的更改无效。 The time returned, after all the slight-of-hand, is identical to the time that went in to it. 在所有轻微的手段之后返回的时间与进入它的时间相同。

You are missing the point here I think. 我想你在这里忽略了这一点。 All PHP runs on the server and so will always reflect the server's version of timezone - that is the expected behaviour in every case. 所有PHP都在服务器上运行,因此将始终反映服务器的时区版本 - 这是每种情况下的预期行为。 If you want to find the client's timezone then you need to query in Javascript and send the reported value back to the server. 如果要查找客户端的时区,则需要在Javascript中查询并将报告的值发送回服务器。

The problem was that the timezone was already 'America/Denver', because I hadn't set it otherwise. 问题是时区已经是'America / Denver',因为我没有另外说过。 In the browser version, it is set elsewhere, but in the sse_bot.php , it wasn't. 在浏览器版本中,它在别处设置,但在sse_bot.php ,它不是。

Just need to add: 只需要添加:

date_default_timezone_set('GMT');

somewhere preceding Code Block 6 , and Bob's your auntie. Code Block 6之前的某个地方,Bob是你的阿姨。

(Probably not answer-worthy, and better suited to a comment, butfuture stumblers will more readily find this one.) (可能不值得回答,更适合评论,但未来的傻瓜会更容易找到这个。)

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

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