如何使用php获取正确的时间

时间:2019-02-04 02:14:48

标签: php

我正在尝试使用php获取正确的时间 例如,不是用户计算机提供的功能就是提供正确“ GMT”时间的功能。

3 个答案:

答案 0 :(得分:2)

PHP获取服务器日期,而不是用户计算机日期。

如果需要获取其他日期时区,则需要将时区设置为所需的时间。

请参阅:http://php.net/manual/en/function.date.php

喜欢:

$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('d.m.Y, H:i:s');

答案 1 :(得分:0)

如果您只需要格林尼治标准时间,则可以使用Firefox: drawImage(video) fails with NS_ERROR_NOT_AVAILABLE: Component is not available代替gmdate。例如,

echo date('Y-m-d H:i:s') . "\n";
echo gmdate('Y-m-d H:i:s') . "\n";

输出(用于欧洲的服务器)

2019-02-04 03:34:05 
2019-02-04 02:34:05

date

答案 2 :(得分:-1)

您将只获取服务器时间,而不是计算机时间

$now = new DateTime(null, new DateTimeZone('America/New_York')); //timezone format
echo $now->format("Y-m-d\TH:i:sO"); // Server time