Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting time zone in php

Tags:

php

time

I'm using the following code to get server time. But I'm in Bangladesh and I don't get Bangladeshi time by this. Please tell me where I have to change for the exact Bangladeshi time.

$Vdate=date("F j, Y, g:i a");
like image 893
Mak Reza Avatar asked Oct 27 '13 10:10

Mak Reza


People also ask

What is PHP default timezone?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts.

How can I modify the PHP timezone setting for my website?

Open Hosting → Manage → PHP Configuration page. There, open the PHP options tab and edit the date. timezone value: If you are not sure which time zone to insert, check the Time Zone Map.

How can I get time zone and country code in PHP?

PHP has a function for it using GeoIP. The geoip_time_zone_by_country_and_region() function will return the time zone corresponding to a country and region code combo.


3 Answers

Function date_default_timezone_set()>= 5.1.0 set timezone globally.

If you need to set timezone locally, for specific variable, you can use DateTime>= 5.2.0 and DateTimezone>= 5.2.0 classes, like:

$dt = new DateTime('now', new DateTimezone('Asia/Dhaka'));
echo $dt->format('F j, Y, g:i a');

Here is the list of all available timezones in PHP.


Since non of the above functions will work on PHP version 4.x, you have no other way to set timezone, rather that setting your server time to your timezone, or add offset to time() functions, like:

echo date('F j, Y, g:i a', time() - 6*3600); # Bangladesh is in UTC+6
like image 132
Glavić Avatar answered Oct 21 '22 03:10

Glavić


You have to use:

date_default_timezone_set('Asia/Dhaka');

I'm not sure if this is the right timezone.

like image 28
Alex Avatar answered Oct 21 '22 04:10

Alex


I think the proper way to set your timezone is to set it in your php.ini configuration file. You just have to set date.timezone = "[Valid timezone value]".

like image 21
steven_ght Avatar answered Oct 21 '22 05:10

steven_ght