在 PHP 中获取服务器时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37429837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get server timezone In PHP
提问by Sahan
My client needs a system report his plugin running server details, It means I have to get SERVER timezone. Working with Wordpress. Tried on this, But this not working on every server.
我的客户需要一个系统报告他的插件运行服务器的详细信息,这意味着我必须获得 SERVER 时区。使用 WordPress。对此进行了尝试,但这不适用于每台服务器。
$date = new DateTime();
$date->setTimezone(new DateTimeZone(ini_get('date.timezone')));
This gives me a fatal error on some servers,
Got any common function to get server timezone?
Any suggestions are greatly appreciated.
这在某些服务器上给了我一个致命错误,是否有任何通用功能来获取服务器时区?
任何建议都非常感谢。
采纳答案by James
Try this:
尝试这个:
$date = new DateTime();
$timeZone = $date->getTimezone();
echo $timeZone->getName();
回答by Kavin D
Kindly refer following link https://www.w3schools.com/php/func_date_default_timezone_get.asp
请参考以下链接 https://www.w3schools.com/php/func_date_default_timezone_get.asp
<?php
echo date_default_timezone_get();
?>
The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.
date_default_timezone_get() 函数返回脚本中所有日期/时间函数使用的默认时区。
回答by uday singh kushwah
<?php
date_default_timezone_set();
if (date_default_timezone_get())
{
echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}
if (ini_get('date.timezone'))
{
echo 'date.timezone: ' . ini_get('date.timezone');
}?>
this code will help you.
此代码将帮助您。
回答by talhasch
get some help from python. you can get server timezone offset with python.
从 python 获得一些帮助。您可以使用python获取服务器时区偏移量。
from time import gmtime, strftime
print strftime("%z", gmtime())
run the python code in php with exec
使用 exec 在 php 中运行 python 代码
$tz_offset = exec("python tz.py");