Change Timezone In PHP

In this tutorial we will let you know about different time zones and how to set them as default zone in PHP.
Change Timezone  In PHP

A time zone is a region on Earth that has a uniform standard time for legal, commercial, and social purposes. It is convenient for areas in close commercial or other communication to keep the same time, so time zones tend to follow the boundaries of countries and their subdivisions.

This is the Description for time zone in Wikipedia

When ever it comes to time its important to make sure that the users get the time displayed in their local time to avoid errors, especially when you have offices or users in different parts of the world.

To make sure that all the user gets all the time displayed in confortable format.

To make that happen you can save the times in default UTC format in database and convert them and display it to the users according to their desired time zone, or if you have all the users from same time Zone, and the time zone is not UTC then you can still follow the same method, you can also red ice the unnecessary server load by setting the default time zone to the desired time in PHP.INI file or to with set default time zone function.

Both the methods are explained here.

date () and time () function

Before we get started with changint time zones , lets take a look at the time() and date() functions.

time() function returns a string formatted according to the given format string using the given integer timestamp, if no format is mentioned it take the current time.

The following code time('d-M-Y',$time) gives an out put in the form DD-MMM-YYY depending on the value of $time.

The following characters can be used in the format parameter string


d 	- Day of the month, 2 digits with leading zeros 	
D 	- A textual representation of a day, three letters                
m 	- Numeric representation of a month with leading zeros 	 
M 	- short textual representation of a month
g 	- 12-hour format without leading zeros 			1 through 12
G 	- 24-hour format without leading zeros		 	0 through 23
h 	- 12-hour format with leading zeros 			01 through 12
H 	- 24-hour format with leading zeros 			00 through 23
a 	-  Ante meridiem and Post meridiem 				am or pm
A 	-  Ante meridiem and Post meridiem 				AM or PM
i 	- Minutes with leading zeros 	
s 	- Seconds, with leading zeros
                

Complte characters that can be used in time from PHP.net

Set Timezone In PHP.ini

This method can only be used on your local server or if you have the permission to edit the PHP.INI file of the server.

You need permission to edit PHP.ini file to do this, which none of the web hosts give you permission.You can use this in your local server or if you have your own Server

To change the default time zone , open the PHP.ini file in any text editor then search for date.timezone, by default it will be set to UTC ,the Latitude's and longitudes will be commented , so delete the Semicolon and Change that to your desired location

date.timezone = "Asia/Delhi"
date.default_latitude = 28.40
date.default_longitude = 77.13

When we tested this method on WAMP server, the PHP gave the following error. Any way it's not recommended to rely on system time.
Requires:PHP 5.0.0.

So this method is not recommended unless you are confident with your self

Longitudes and latitudes of a number of major cities


// --- Asia --- //

// Baku, Azerbaijan, Asia
date.timezone = "Asia/Baku"
date.default_latitude = 40,2343
date.default_longitude = 49,5256

// Shanghai, China, Asia
date.timezone = "Asia/Shanghai"
date.default_latitude = 31.5167
date.default_longitude = 121.4500

// Delhi, India, Asia
date.timezone = "Asia/Delhi"
date.default_latitude = 28.40
date.default_longitude = 77.13

// Colombo, Sri Lanka, Asia
date.timezone = "Asia/Colombo"
date.default_latitude = 6.9319444
date.default_longitude = 79.8477778

// --- Australia --- //

// Canberra, Australia
date.timezone = "Australia/Canberra"
date.default_latitude = -35.308142
date.default_longitude = 149.124518

// --- Europe --- //

// London, England, Europe
date.timezone = "Europe/London"
date.default_latitude = 51.500181
date.default_longitude = -0.12619

// Berlin, Germany, Europe
date.timezone = "Europe/Berlin"
date.default_latitude = 52.5194
date.default_longitude = 13.4067

// Madrid, Spain, Europe
date.timezone = "Europe/Madrid"
date.default_latitude = 40.416126
date.default_longitude = -3.696706				
				

Change Timezone date_default_timezone_set( )

This method is pretty straight forward as there is no much configuration needed. Just make a call to date_default_timezone_set( ) function with the requires time zone as argument in single quotes, all the date() following below will display the time in the requested time zone. The date() or time() mentioned above the function call will display the UTC time or as configured in PHP.ini file.


echo "Original Time: ". date("h:i:s")."\n"
date_default_timezone_set('Asia/Kolkata');
echo "Indian Standart time Time: ". date("h:i:s")."\n";
                

The Output for above code is given below. it displays the time in Indian Standard time(IST)


Original Time: 10:45:23
Indian Standart time Time: 04:15:23                 
                

Using the above two functions You cannot change the time stored in database to your desired one.

Convert Time in One Zone to Another Zone

If you have time stored in your data base in some other zone and you need that in another, you cannot achieve that with the above things, even if you have users using different Time zones, you can do that with this as the user can set the desired time as he needs it.

To get that done you need to make a standard in storing the time in the database. We recommend to use UTC as its will be easier for conversion. Any format will do fine but you may need to do some more works to get the conversion done.

Given below is a sample code on how to change the time from one zone to another, you can change the zone to desired one to use it.


$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d H:i:sP') . "\n";

                

All Time zones supported in PHP from PHP.net

Previous:
Next:
Anush

Anush

Anush is a freelance web designer and developer with a passion for interaction design. He founded Tech Stream in mid 2011 to spread the art of web design and programing Skills.

  • Social Links:
comments powered by Disqus