Tag Archives: timezone

Ubuntu Server: How to change or set the Timezone Tips, Tricks and Tutorials 29 SEP 2017

Dropping this as a quick reminder to myself. When confronted with a new Ubuntu server instance, I can check the currently set time zone by simply asking for the current time with


Now if I need to change the currently set time zone to something else, the easiest would be to just run

sudo dpkg-reconfigure tzdata

This should bring up an easy to use graphical interface that will allow you to select the correct geographical region/city time zone that you wish to have your server reflect.

Note: Because your cron jobs all depends on this information, it is a VERY good idea to restart the cron service after making your timezone change:

sudo service cron restart

Also worth pointing out, if you are not particularly sure on which city time zone to choose, you could always select the geographic area “Etc” and then choose from the more generic options like GMT or GMT+2.

(UPDATE: Oh. Turns out that that I’ve left this note for myself before. I may need to rethink my note system.)

Related Link: Ubuntu Server

How to Fix PHP Warning: “It is not Safe to rely on the System’s Timezone Settings” Programming 26 FEB 2013

At some point in time with the newer versions of PHP, a lot of you would see this warning spit out by your PHP installation every time you called a date related function:

“It is not safe to rely on the system’s timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘UTC’ for ‘Africa/Johannesburg’ instead”.

What it is basically telling you is that you need to explicitly set the timezone which PHP is to use, instead of just letting it run with whatever the server was using as you used to do in the past. To do this you have two choices, which if you had bothered reading the warning message, would have been given to you on a platter.

The first option you would use on a script by script basis, which of course doesn’t really make all that much sense. Basically, call the date_default_timezone_set() function and set it to a timezone string of your choice.

The second option, and the one which makes the most sense is to set the default timezone in the php.ini configuration file itself, using the date.timezone declaration. In practice:

date.timezone = 'Africa/Johannesburg'


Related Link: http://php.net/manual/en/function.date-default-timezone-set.php

Ubuntu: How to set PHP’s default Time Zone CodeUnit 21 FEB 2011

From PHP 5.1 onwards, you have been able to define the default time zone which is to be used in PHP regardless of what time zone the server or Apache might be using. To do this requires some minor adjustments to your php.ini config file.

First, access your php.ini file with:

sudo nano /etc/php5/apache2/php.ini

Locate the Module Settings section, specifically [Date] in the php.ini file (Mine was around line 945). You will see a “;date.timezone = ” entry. Uncomment the line by removing the ; and then add to it the timezone you wish to use. I wanted to set for GMT, so my uncommented line looks like this:

date.timezone = Etc/GMT

You can find a list of available time zones here: http://www.php.net/manual/en/timezones.php

Save your changes and reload apache (I like doing this just to be sure):

sudo service apache2 restart


How to Change the MySQL Time Zone on an Ubuntu Server CodeUnit 18 FEB 2011

If you wish to explicitly set the time zone for your MySQL running on an Ubuntu Server, here is how to do it.

First open your global MySQL my.cnf configuration file:

sudo nano /etc/mysql/my.cnf

Scroll down the file to location the [mysqld_safe] section. Add the following line:

timezone = GMT

Obviously you would set it to what you wanted to use, be it UTC, GMT+2 or whatever. Save your changes and restart MySQL:

sudo service mysql restart


(You can check by creating a table with a timestamp column and then inserting a record with NOW() as the value. This should then show up correctly when queried.)

How to Change the Time Zone on an Ubuntu Server CodeUnit 17 FEB 2011

It is often quite useful to be able to change the time zone on your Ubuntu server and to do so has become particularly simple. Now that the original command, tzconfig has been deprecated, we turn to the new option:

sudo dpkg-reconfigure tzdata

Running this brings up a blue screen installer prompt that guides you through selecting a new time zone for your server by first selecting a geographic area and then narrowing to a resulting list of cities in that area, with each city representing the time zone in which it is located.

If you aren’t particularly sure on a city time zone to select, you can also set your Ubuntu server time zone to a more direct setting by first selecting the geographic area “Etc” and then choosing from the more generic options like GMT or GMT+2.


PHP: Determine the Offset between two Time Zones Programming 07 SEP 2009

Because I keep forgetting where I put this code fragment on my system, grabbed from the comments section from the PHP online manual just by the way, I may as well just pop it on here so that I can keep track of it better in the future.

The function you see below is useful for determining the offset (in seconds) between your local (origin) timezone and a remote timezone. In order to run, it requires a remote timezone region string, as well as a second timezone region string if you don’t wish to use the system’s preset timezone. (And if you’re looking for timezone regions strings, may I suggest the excellent http://www.worldtimezones.com as a source?)

So, without further ado, here’s the nifty code:

/**    Returns the offset from the origin timezone to the remote timezone, in seconds.
*    @param $remote_tz;
*    @param $origin_tz; If null the servers current timezone is used as the origin.
*    @return int;

function get_timezone_offset($remote_tz, $origin_tz = null) {
    if($origin_tz === null) {
        if(!is_string($origin_tz = date_default_timezone_get())) {
            return false; // A UTC timestamp was returned -- bail out!
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
    return $offset;

And what good would the code be without a nice example to go along with it:

// This will return 10800 (3 hours) ...
$offset = get_timezone_offset('America/Los_Angeles','America/New_York');
// or, if your server time is already set to 'America/New_York'...
$offset = get_timezone_offset('America/Los_Angeles');
// You can then take $offset and adjust your timestamp.
$offset_time = time() + $offset;

It’s clean, it works and I like it, so kudos to the person named Dan at authenticdesign.net then! :)

wall clocks for different time zones