PHP date_default_timezone_set()

断了今生、忘了曾经 提交于 2019-12-22 04:38:16

问题


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.

provided i don have access to server, only PHP files. Thanks in advance

UPDATE

i am using apache (LAMP), and don't have access to php.ini


回答1:


Considering you use apache from the fact you mention .htaccess:

Yes, as long as it runs mod_php it is possible in .htaccess like so:

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.




回答2:


Set date.timezone in your php.ini. Supported timezone values: http://php.net/manual/en/timezones.php




回答3:


I was getting 500 (Internal server Error) using the code

php_value date.timezone "Europe/Berlin".

Then I tried, SetEnv TZ Australia/Melbourne and it worked like a charm.




回答4:


If you get a 500 error you might try checking for the PHP5 module, works for me.

<IfModule mod_php5.c>
php_value date.timezone "Europe/Lisbon"
</IfModule>



回答5:


After adding below code in .htaccess file from CPanel Server.
I used to get 500 (Internal server Error)

#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.ini then include
date.timezone=Asia/Kolkata in php.ini file


Here is the complete php.ini file

; 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.




回答6:


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 .htaccess you 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:

.htaccess

php_value auto_prepend_file "path/of/file.php"


path/of/file.php

date_default_timezone_set(ini_get('date.timezone'));


来源:https://stackoverflow.com/questions/13916481/php-date-default-timezone-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!