これに対する解決策は Javascript です。クライアントのタイム ゾーン設定にアクセスできます。残念ながら、特定の日付のタイム ゾーン オフセット (分単位で指定) しか取得できず、タイム ゾーン名は取得できません。したがって、正しいタイム ゾーンを特定するには、夏時間 (DST) が採用されているかどうかも知る必要があります。これは、ソリューションのクライアント側の部分です。
var now = new Date();
var later = new Date();
// Set time for how long the cookie should be saved
later.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
// Set cookie for the time zone offset in minutes
setCookie("time_zone_offset", now.getTimezoneOffset(), later, "/");
// Create two new dates
var d1 = new Date();
var d2 = new Date();
// Date one is set to January 1st of this year
// Guaranteed not to be in DST for northern hemisphere,
// and guaranteed to be in DST for southern hemisphere
// (If DST exists on client PC)
d1.setDate(1);
d1.setMonth(1);
// Date two is set to July 1st of this year
// Guaranteed to be in DST for northern hemisphere,
// and guaranteed not to be in DST for southern hemisphere
// (If DST exists on client PC)
d2.setDate(1);
d2.setMonth(7);
// If time zone offsets match, no DST exists for this time zone
if(parseInt(d1.getTimezoneOffset())==parseInt(d2.getTimezoneOffset()))
{
setCookie("time_zone_dst", "0", later, "/");
}
// DST exists for this time zone – check if it is currently active
else {
// Find out if we are on northern or southern hemisphere
// Hemisphere is positive for northern, and negative for southern
var hemisphere = parseInt(d1.getTimezoneOffset())-parseInt(d2.getTimezoneOffset());
// Current date is still before or after DST, not containing DST
if((hemisphere>0 && parseInt(d1.getTimezoneOffset())==parseInt(now.getTimezoneOffset())) ||
(hemisphere<0 && parseInt(d2.getTimezoneOffset())==parseInt(now.getTimezoneOffset()))) { setCookie("time_zone_dst", "0", later, "/"); } // DST is active right now with the current date else { setCookie("time_zone_dst", "1", later, "/"); } }
結果を Cookie として保存し、PHP スクリプトからアクセスできます。上記のコードは、少なくともユーザーがアクセスする最初のページに含める必要があります。セッション中にそのような変更が発生する可能性が低い場合でも、変更を認識 (および適応) するために、すべてのページにコードを含めます。
PHP では、PHP 5.1.3 以降で利用可能な timezone_name_from_abbr という名前の新しい関数を使用して、有効なタイム ゾーンを抽出できます。タイム ゾーンの省略形、またはタイム ゾーン オフセット (秒単位) と夏時間の組み合わせのいずれかを使用します。後者の組み合わせ:
$time_zone_name = timezone_name_from_abbr(", -$_COOKIE['time_zone_offset']*60, $_COOKIE['time_zone_dst']);
これにより、Cookie のデータが有効であれば、ユーザーの正しいタイム ゾーン名が得られます。たとえば、「ヨーロッパ/ベルリン」や「ヨーロッパ/チューリッヒ」など、正確な名前を持つ「重複した」名前が多数あることに注意してください。同じタイム ゾーン設定 (少なくとも今のところ) であり、適切なオフセットおよび DST 変数に対してそれらのいずれかを取得できます。タイム ゾーン名のリストは、php.net のサポートされているタイム ゾーンのリストにあります。
特定のタイム ゾーンで日付文字列を作成
する ユーザーのタイム ゾーンの名前を使用して、PHP クラスの DateTimeZone と DateTime を使用して、最終的に正しいタイム ゾーンで日付文字列を作成できます。
// Create time zone class
$time_zone_class = new DateTimeZone($time_zone_name);
// Create new date class with a given date
// Notice that the provided date will be regarded as being in the
// default time zone and converted accordingly
$new_date = new DateTime(‘2007-02-14 15:30:00′, $time_zone_class);
// Print date with the user’s time zone echo $new_date->format(‘Y-m-d H:i:s’);
それでおしまい!
ソース: http://togl.me/eE2