demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

Czech Republic, The Prague Orloj

Changing The PHP Default Timezone

Every installation of PHP makes an assumption about where it is on planet Earth. PHP does not read the location information from the computer it is installed on: that would be the role of JavaScript. Instead, every web server installation defaults to a pre-determined timezone. It is likely that this default won’t match the actual physical location of the server. For example, if you’ve downloaded and installed XAMPP, the server assumes its local timezone is Berlin, Germany.

This can be confusing, especially when using the date() function on a PHP page – date("Y") will obviously provide the right year 99.99% of the time, but date('l’) (a lowercase L, to generate the name of the day) will sometimes be right for your location (when Germany happens to be in the same day) and sometimes not (when Germany is one day ahead of you)*.

While you can look through the php.ini file or phpinfo() to locate the default timezone information, most people will probably find it easier to display it on a PHP page with a temporary line of code:

  1. <?php echo date_default_timezone_get(); ?>

This will provide you with the geographical area that PHP assumes it is located in. Note that this information is provided in the format city/region. If "America" is referred to, it includes the entire continent, from the frozen north of Nunavut to Tierra del Fuego. Also note that the city location references the primary or capital city of the area, and does not recognize inter-city or national rivalries, so (for example) the correct timezone for Alberta is Edmonton, America.

If the timezone displayed on this test page is not correct, you can try editing the server’s php.ini file, but this may not be possible in a hosted web environment. Most people will find it easier to correct the timezone on the PHP page itself, before you calculate a date or time.

For any PHP5 script that needs to reference the exact local time on a server with an incorrect timezone, before you use any date or time functions, write:

  1. <?php date_default_timezone_set('America/Edmonton'); ?>

(php.net has a complete list of PHP timezone values: find the one nearest and most relevant to you, replacing 'America/Edmonton' with the appropriate location).

If you follow this line with echo date_default_timezone_get() you’ll find that the timezone is now correct, as are all calculated dates. You just have to remember to start any page that uses PHP-generated time or date values with this line. It’s not unusual to ensure this by making it part of an include for every page:

  1. <? date_default_timezone_set('America/Edmonton'); ?>
  2. <!DOCTYPE html>
  3. <head>

With this in place, confusing time results in PHP should be a thing of the past.

*this, of course, assumes you are not in Germany

You must be signed up in order to leave comments.

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.