Thursday, October 8, 2009

How do I get local date & time with the PHP date & time function?

PHP's int time() function returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). The string date ( string format [, int timestamp] ) returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time(). If you want to have a manual date timezone convertion see the code below.

Example

<?php 
    $gmt_timezone_offset = +8;
    $time = time();
    $my_gmt_timezone = $time + ($gmt_timezone_offset * 60 * 60);
    echo date("Y-m-d H:i:s", $my_gmt_timezone);
?>

Or Simply, you can do this:
<?php 
    // put this code at the very top of your file
    ini_set('date.timezone', "Asia/Taipei");
    ...
?>

Or you can modify php.ini.

...
date.timezone = "Asia/Taipei"
...

Or you can modify .htaccess
...
php_value date.timezone Asia/Taipei
# or
php_value date.timezone UTC
...

The following table indicates each timezone and its location. Below are the list of timezone reference:

http://php.net/manual/en/timezones.php

OR

Time Zone Location
UM12 (UTC - 12:00) Enitwetok, Kwajalien
UM11 (UTC - 11:00) Nome, Midway Island, Samoa
UM10 (UTC - 10:00) Hawaii
UM9 (UTC - 9:00) Alaska
UM8 (UTC - 8:00) Pacific Time
UM7 (UTC - 7:00) Mountain Time
UM6 (UTC - 6:00) Central Time, Mexico City
UM5 (UTC - 5:00) Eastern Time, Bogota, Lima, Quito
UM4 (UTC - 4:00) Atlantic Time, Caracas, La Paz
UM25 (UTC - 3:30) Newfoundland
UM3 (UTC - 3:00) Brazil, Buenos Aires, Georgetown, Falkland
Is.
UM2 (UTC - 2:00) Mid-Atlantic, Ascention Is., St Helena
UM1 (UTC - 1:00) Azores, Cape Verde Islands
UTC (UTC) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia
UP1 (UTC + 1:00) Berlin, Brussels, Copenhagen, Madrid, Paris,
Rome
UP2 (UTC + 2:00) Kaliningrad, South Africa, Warsaw
UP3 (UTC + 3:00) Baghdad, Riyadh, Moscow, Nairobi
UP25 (UTC + 3:30) Tehran
UP4 (UTC + 4:00) Adu Dhabi, Baku, Muscat, Tbilisi
UP35 (UTC + 4:30) Kabul
UP5 (UTC + 5:00) Islamabad, Karachi, Tashkent
UP45 (UTC + 5:30) Bombay, Calcutta, Madras, New Delhi
UP6 (UTC + 6:00) Almaty, Colomba, Dhaka
UP7 (UTC + 7:00) Bangkok, Hanoi, Jakarta
UP8 (UTC + 8:00) Beijing, Hong Kong, Perth, Singapore, Taipei
UP9 (UTC + 9:00) Osaka, Sapporo, Seoul, Tokyo, Yakutsk
UP85 (UTC + 9:30) Adelaide, Darwin
UP10 (UTC + 10:00) Melbourne, Papua New Guinea, Sydney, Vladivostok
UP11 (UTC + 11:00) Magadan, New Caledonia, Solomon Islands
UP12 (UTC + 12:00) Auckland, Wellington, Fiji, Marshall Island

Related Posts




1 comment:

  1. Does this script keep Daylight savings in mind? Or should that even be an issue?

    (Meaning your first example there...) For some reason or another it's one hour off for me... Not sure.. strange/

    ReplyDelete