Dollar Money Woof Woof
Dollar Money Woof Woof

Reputation: 1068

PHP date_default_timezone_set()

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

Upvotes: 7

Views: 22631

Answers (6)

user4244405
user4244405

Reputation:

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'));

Upvotes: 0

Nɪsʜᴀɴᴛʜ ॐ
Nɪsʜᴀɴᴛʜ ॐ

Reputation: 2904

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.

Upvotes: 0

user2188875
user2188875

Reputation:

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>

Upvotes: 1

Fallen
Fallen

Reputation: 4565

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.

Upvotes: 2

Hikaru-Shindo
Hikaru-Shindo

Reputation: 1901

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.

Upvotes: 11

Karl Laurentius Roos
Karl Laurentius Roos

Reputation: 4399

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

Upvotes: 3

Related Questions