In my case, I'm not sure I can guess the correct timezone any better than PHP and it's no where near important enough to nag the user, so...
<?php
// Suppress DateTime warnings
date_default_timezone_set(@date_default_timezone_get());
?>
date_default_timezone_get
(PHP 5 >= 5.1.0)
date_default_timezone_get — スクリプト中の日付/時刻関数で使用されるデフォルトタイムゾーンを取得する
説明
この関数は、デフォルトのタイムゾーンを以下の優先順位で取得して返します。
-
date_default_timezone_set() 関数を使用して 設定したタイムゾーン (もし何か設定されていれば) を読み込む
-
PHP 5.4.0 より前のバージョンのみ: TZ 環境変数 (もし空白でなければ) を読み込む
-
date.timezone ini オプション (設定されていれば) を読み込む
-
PHP 5.4.0 より前のバージョンのみ: ホスト OS に問い合わせる (もし OS がそれに対応しており、許可されていれば)。 これは、タイムゾーンを推測するアルゴリズムを使います。 このアルゴリズムはあらゆる状況で正しく動作するとは限りません。 この段階に到達したときには警告が表示されます。 この推測が正しくなることに期待するのではなく、 date.timezone に正しいタイムゾーンを設定するようにしましょう。
上のすべてが失敗した場合は、 date_default_timezone_get() はデフォルトのタイムゾーンである UTC を返します。
返り値
string を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.4.0 | タイムゾーンの判定時に環境変数 TZ を使わなくなりました。 |
| 5.4.0 | タイムゾーンの判定時に、OS から得られる情報に頼らないようになりました。 推測に基づくタイムゾーンは信頼できないからです。 |
例
例1 デフォルトのタイムゾーンの取得
<?php
date_default_timezone_set('Europe/London');
if (date_default_timezone_get()) {
echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}
if (ini_get('date.timezone')) {
echo 'date.timezone: ' . ini_get('date.timezone');
}
?>
上の例の出力は、 たとえば以下のようになります。
date_default_timezone_set: Europe/London date.timezone: Europe/London
例2 タイムゾーンの短縮名の取得
<?php
date_default_timezone_set('America/Los_Angeles');
echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');
?>
上の例の出力は以下となります。
America/Los_Angeles => America/Los_Angeles => PST
If you want to get the abbrivation (3 or 4 letter), instead of the long timezone string you can use date('T') function like this:
Input:
date_default_timezone_set('America/Los_Angeles');
echo date_default_timezone_get();
echo ' => '.date('e');
echo ' => '.date('T');
Output:
America/Los_Angeles => America/Los_Angeles => PST
date_default_timezone_get() will still emit a warning in E_STRICT if the timezone is not set; either by date_default_timezone_set() or the ini option of date.timezone.
This is probably not a big deal, but I thought I would contribute what I found.
