PHP date_default_timezone_set()

2019-04-25 16:59发布

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

标签: php timezone
6条回答
Anthone
2楼-- · 2019-04-25 17:37

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'));
查看更多
beautiful°
3楼-- · 2019-04-25 17:51

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楼-- · 2019-04-25 17:58

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

查看更多
等我变得足够好
5楼-- · 2019-04-25 17:59

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.

查看更多
The star\"
6楼-- · 2019-04-25 18:01

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>
查看更多
劳资没心,怎么记你
7楼-- · 2019-04-25 18:03

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.

查看更多
登录 后发表回答