PHP date_default_timezone_set()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13916481/
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
PHP date_default_timezone_set()
提问by Tian Loon
In php, is there a way to set default timezone in .htaccess or wherever, as long as i don have to set it at every php page.
在 php 中,有没有办法在 .htaccess 或任何地方设置默认时区,只要我不必在每个 php 页面设置它。
provided i don have access to server, only PHP files. Thanks in advance
如果我无权访问服务器,只能访问 PHP 文件。提前致谢
UPDATE
更新
i am using apache (LAMP), and don't have access to php.ini
我正在使用 apache (LAMP),但无权访问 php.ini
回答by Hikaru-Shindo
Considering you use apache from the fact you mention .htaccess:
考虑到您从提到 .htaccess 的事实使用 apache:
Yes, as long as it runs mod_php it is possible in .htaccess like so:
是的,只要它运行 mod_php 就可以在 .htaccess 中像这样:
php_value date.timezone "Europe/Berlin"
Or you could set date.timezone in php.ini like Karl Laurentius Roos suggested. This would only be possible if you have access to your php config through. Remember to restart PHP (CGI mode) or your webserver (mod_php) after altering php.ini.
或者您可以像 Karl Laurentius Roos 建议的那样在 php.ini 中设置 date.timezone。这只有在您有权访问您的 php 配置时才有可能。记住在更改 php.ini 后重新启动 PHP(CGI 模式)或您的网络服务器(mod_php)。
回答by Karl Laurentius Roos
Set date.timezonein your php.ini. Supported timezone values: http://php.net/manual/en/timezones.php
date.timezone在你的 php.ini 中设置。支持的时区值:http: //php.net/manual/en/timezones.php
回答by Fallen
I was getting 500 (Internal server Error) using the code
我使用代码收到 500(内部服务器错误)
php_value date.timezone "Europe/Berlin".
php_value date.timezone "Europe/Berlin".
Then I tried, SetEnv TZ Australia/Melbourneand it worked like a charm.
然后我尝试了,SetEnv TZ Australia/Melbourne它就像一个魅力。
回答by Fallen
If you get a 500 error you might try checking for the PHP5 module, works for me.
如果您收到 500 错误,您可以尝试检查 PHP5 模块,这对我有用。
<IfModule mod_php5.c>
php_value date.timezone "Europe/Lisbon"
</IfModule>
回答by N?s????? ?
After adding below code in .htaccessfile from CPanel Server.
I used to get 500 (Internal server Error)
在CPanel 服务器的.htaccess文件中添加以下代码后。
我曾经得到 500(内部服务器错误)
#Adjust default time zone
<IfModule mod_php5.c>
php_value date.timezone "Asia/Kolkata"
</IfModule>
SetEnv TZ Asia/Kolkata
Then I navigated to the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor) >> Edit PHP INI settings
OR
Go to the Path /homecws/Your_folder_name/public_html/php.inithen
include date.timezone=Asia/Kolkatain php.inifile
然后我导航到cPanel MultiPHP INI 编辑器(主页 >> 软件 >> MultiPHP INI 编辑器)>> 编辑 PHP INI 设置
或
转到路径/homecws/Your_folder_name/public_html/php.ini然后包含date.timezone=Asia/Kolkata在php.ini文件中
Here is the complete php.ini file
这是完整的 php.ini 文件
; magic_quotes_gpc = Off;
; register_globals = Off;
; default_charset = UTF-8;
; memory_limit = 64M;
; max_execution_time = 36000;
; upload_max_filesize = 999M;
; safe_mode = Off;
; mysql.connect_timeout = 20;
; session.auto_start = Off;
; session.use_only_cookies = On;
; session.use_cookies = On;
; session.use_trans_sid = Off;
; session.cookie_httponly = On;
; session.gc_maxlifetime = 3600;
; allow_url_fopen = on;
; ;display_errors = 1;
; ;error_reporting = E_ALL;
allow_url_fopen = On
allow_url_include = On
asp_tags = Off
display_errors = On
enable_dl = Off
file_uploads = On
max_execution_time = 300
max_input_time = 600
max_input_vars = 10000
memory_limit = 512M
session.gc_maxlifetime = 14400
session.save_path = "/var/cpanel/php/sessions/ea-php56"
upload_max_filesize = 2000M
post_max_size = 8M
zlib.output_compression = On
date.timezone = Asia/Kolkata
This solved and worked for mine.
这解决了并为我工作。
回答by N?s????? ?
If the server is properly configured and you want to set your process time-zone to this server value (if not set), then in the doc-root .htaccessyou can set a PHP file to run before any other PHP file -and in this file you can define a couple of configurations, functions and classes to use as "tools" which will be available in all the other PHP files - like this:
如果服务器配置正确,并且您希望将进程时区设置为此服务器值(如果未设置),则在 doc-root 中,.htaccess您可以设置一个 PHP 文件在任何其他 PHP 文件之前运行 - 在此文件中您可以定义一些配置、函数和类来用作“工具”,它们将在所有其他 PHP 文件中可用 - 如下所示:
.htaccess
.htaccess
php_value auto_prepend_file "path/of/file.php"
path/of/file.php
路径/of/file.php
date_default_timezone_set(ini_get('date.timezone'));

