HomeCoding & Programming

PHP timezone setting using date_default_timezone_set function

Like Tweet Pin it Share Share Email

If you have hosted your website on a server that is not in the same time zone as you live. In the old days of PHP3 & PHP4, we would write a time_offset function or something like that to roll back or add the necessary hours to get the time correct for your own use (hard stuff).

 

Since PHP 5.1.0, you have the function date_default_timezone_set() available. One simply needs to call this with your own timezone at the top of their PHP file, that’s all.

 

date_default_timezone_set function sets the default timezone used by all date/time functions in script.
The function returns boolean value, false if the timezone_identifier isn’t valid, otherwise true.

 

you can get the valid timezones by click here

 

Syntax

bool date_default_timezone_set ( string $timezone_identifier )

It is notable that default timezone can also be set by using php.ini setting date.timezone directive.
The timezone_identifier refers to the Coordinated Universal Time (UTC) or Greenwich Mean Time (GMT) or region based time like India/Kolkata.

You might need to restart your web server for the changes to take effect if you choose this one.

 

If you want to set timezone using .htaccess here is way you can go

php_value date.timezone “India/Kolkata”


<?php

date_default_timezone_set('India/Kolkata');

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

?>

 

Guys, post me comments if you found above one script is not working and want to improve the above!!