Setting the Default Timezone in PHP

You may have often wondered why times on some webpages seem to be incorrect. The most common ones I come across are on forums where the local time and server time are different and the user (usually me!) has not set their preferences correctly. 

Apart from the end user, there are other considerations for developers. The actual location and time on the server may not be the same as the location of the website. This can be adjusted by setting the server clock to match the website location but a preferred method is to set the timezone for each site according to its location.

For example, a server based in Texas, USA may host several websites from various countries.  If one of those sites is in Brisbane, Australia and the site is using time functions then it would be useful to display the correct times to users, especially if those users are expected to be local Brisbane users.

We can use the default timezone function in PHP to help with this:

<?php

date_default_timezone_set('Australia/Brisbane');

There are other time functions which you may find beneficial.

If you would like to learn more about PHP then enrol in the PHP for Beginners course.

Want to Learn More?

Become a member and get access to the member only area of this site here.

Comments

Your email address will not be published.

PHP for Beginners

Enrol in the full course here

PHP, MySQL and PDO

Coming soon, a full course on using PHP, MySQL and PDO. Be notified as soon as the course goes live.