Tag Archives: time zone

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

date

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

PHP: How to Convert a Date/Time from UTC to another Time Zone Programming 27 MAR 2015

Having previously shown you a useful way of quickly converting a timestamp into a formatted date string, here’s a quick way of actually getting a date/time in a time zone that you actually want – and again we turn to the powerfull PHP DateTime class to do this for us.

//here we'll use a UTC (time zone independent) time for our example
//(remember, time() always returns a UTC timestamp)
$dateTimeEnd = new DateTime('@' . time());
echo $dateTimeEnd->format('Y-m-d H:i:s');            
//now let's change to GMT+2 (SAST - South African Standard Time)
$dateTimeEnd->setTimezone(new DateTimeZone('Africa/Johannesburg'));
echo $dateTimeEnd->format('Y-m-d H:i:s');

As you can see, the setTimezone function of the DateTime class does all the heavy lifting for us, making it a snap to convert timestamps between different time zones.

Just a note, if you want to get the current time zone that your script is executing under, you can make use of date_default_timezone_get:

if (date_default_timezone_get()) {
    echo 'current time zone: ' . date_default_timezone_get() . '<br />';
}

A handy tip to remember then in other words!

world map showing time zones

Related Link: http://php.net/manual/en/class.datetime.php

Ensure that an Ubuntu PC’s Time Stays Correct and Up-to-date CodeUnit 12 OCT 2011

Some older machines suffer from a system clock that systematically loses time, resulting in your operating system, in this case Ubuntu, displaying the incorrect time and in the process causing you to be late for all you carefully scheduled appointments, unless of course you have a second time source on hand.

One way to avoid this is to force Ubuntu to keep its clock synchronized with Internet servers, meaning a quick check on boot up, and a time that should always be pretty accurate, unless of course you don’t have network connection and your system clock failed miserable at the last power down sequence.

To install NTP, simply open up a terminal window and execute:

sudo apt-get install ntp

Reboot your machine and voila, your clock should now be reflecting an accurate time for your selected time zone! :)

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

Done.

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

Done.

(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.

Nifty

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