Cara menggunakan php central time zone

Apakah ada daftar zona waktu untuk Amerika Serikat yang bisa saya gunakan dengan php? Saya menggunakan nama zona waktu dari php seperti America/Anchorage, php mencantumkan zona waktu di sini:

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

Saya ingin menampilkannya secara drop-down agar pengguna dapat memilih, tetapi saya hanya membutuhkan yang untuk AS, saya tidak ingin menunjukkan banyak yang berada di luar AS.

Berikut adalah daftar yang saya temukan:

Eastern Time    America/New_York
Central Time    America/Chicago
Mountain Time   America/Denver
Mountain Time (no DST) America/Phoenix
Pacific Time    America/Los_Angeles
Alaska Time America/Anchorage
Hawaii-Aleutian America/Adak
Hawaii-Aleutian Time (no DST) Pacific/Honolulu

Seperti kata deceze, Anda dapat menggunakan flag PER_COUNTRY.

$timezone_identifiers = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US');
foreach($timezone_identifiers as $timezone_identifier) {
  echo "$timezone_identifier\n";
}

Keluaran:

America/Adak
America/Anchorage
America/Boise
America/Chicago
America/Denver
America/Detroit
America/Indiana/Indianapolis
America/Indiana/Knox
America/Indiana/Marengo
America/Indiana/Petersburg
America/Indiana/Tell_City
America/Indiana/Vevay
America/Indiana/Vincennes
America/Indiana/Winamac
America/Juneau
America/Kentucky/Louisville
America/Kentucky/Monticello
America/Los_Angeles
America/Menominee
America/Metlakatla
America/New_York
America/Nome
America/North_Dakota/Beulah
America/North_Dakota/Center
America/North_Dakota/New_Salem
America/Phoenix
America/Shiprock
America/Sitka
America/Yakutat
Pacific/Honolulu

Ini akan secara otomatis memberi Anda daftar zona waktu Amerika:

if (defined('DateTimeZone::AMERICA')) {
    // PHP 5.3+
    $timezoneIdentifiers = timezone_identifiers_list(DateTimeZone::AMERICA);
} else {
    // PHP 5.2
    $timezoneIdentifiers = timezone_identifiers_list();
    $timezoneIdentifiers = array_filter($timezoneIdentifiers, create_function('$tz', 'return preg_match("/^America\//", $tz);'));
}

Perhatikan bahwa ini akan mencakup zona waktu Amerika Selatan, Kanada dll. Anda bisa bermain-main dengan DateTimeZone::PER_COUNTRY sebagai filter ke timezone_identifiers_list untuk PHP 5.3 dan melihat apakah itu sesuai keinginan Anda, atau filter daftar lengkap untuk US/. Pilihan terbaik mungkin adalah untuk memilih sendiri zona waktu.

Cara kasar untuk mendapatkan SEMUA zona waktu per negara menggunakan GEOIP PHP. Kode ini tidak dimaksudkan untuk terlihat bagus. Anda harus memanggil ini hanya sekali dan menyimpan hasilnya di suatu tempat.

$timezones = array();

foreach (range('A', 'Z') as $i) {
    foreach (range('A', 'Z') as $j) {
        $country = $i . $j;

        foreach (range('A', 'Z') as $k) {
            foreach (range('A', 'Z') as $l) {
                $region = $k . $l;

                if ($timezone = geoip_time_zone_by_country_and_region($country, $region)) {
                    $timezones[$country][$timezone] = true;
                }
            }
        }

        foreach (range(0, 99) as $m) {
            if ($timezone = geoip_time_zone_by_country_and_region($country, sprintf('%02d', $m))) {
                $timezones[$country][$timezone] = true;
            }
        }
    }
}

var_dump($timezones);

Bagi anda yang mempunyai masalah atau warning funtion.date, berikut cara setting Time Zone untuk PHP 5.3.0,
biasanya error yang dijumpai di website adalah seperti ini:

    Warning: date() [function.date]: 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 ‘Asia/Jakarta’ for ‘WIT/7.0/no DST’ instead in /home/xxxxxx/public_html/config.php on line 99

dari error diatas dapat diartikal bahwa ada kesalahan pada timezone setting jadi kita harus melakukan setting sesuai yang di infokan.

    We selected ‘Asia/Jakarta’ for ‘WIT/7.0/no DST’ instead in /home/xxxxxx/public_html/config.php on line 99

ini menandakan bahwa skrip website anda memilih setting waktu ‘Asia/Jakarta’  sehingga kita harus menyesuaikan dengan setting waktu yang ada di skrip  website kita.

Caranya adalah sebagai berikut:

Buka file php.ini, jika file blm ada anda bisa create new file php.ini

    date.timezone = {tulis timezone disini}

contohnya bila di indonesia menggunakan time zone berikut:

    date.timezone =  Asia/Jakarta

kemudian Save file php.ini dan selesai

If you notice the incorrect time in your PHP scripts, the likely culprit is that the hosting server is in a different timezone.

For example, when you’re an InMotion web hosting customer, you can select from data centers in Washington, D.C., or California, which means the server’s timezone will be either Eastern Standard Time (EST) or Pacific Standard Time (PST).

If you want to change that, it’s a quick fix. Simply insert a line of code into your php.ini file and the location you set in the code will determine the time to display in your PHP scripts. Keep reading to learn how.

  1. Open your php.ini file with the File Manager in cPanel .
  2. Add the following line of code to top of your php.ini file: date.timezone = "US/Central".

  3. Replace US/Central with the timezone from here that corresponds to the time you want to display.
  4. Once you have entered the desired timezone, click Save Changes to save the file.

  5. Now, you can check your phpinfo.php page to verify the change took place.

Drag the arrow to the right to see the change in the timezone setting

Congratulations! You are now able to set the timezone in PHP. If you wish to see more guides with tips and information for your website issues, please see our InMotion Hosting Support Center!

Sell hosting using your own brand with a powerful Reseller Hosting plan, now with more resources and faster storage using SSDs at no extra cost.

Arnel Custodio Content Writer I

As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.

More Articles by Arnel