0

bigint2013 年 8 月 11 日 - 午後 11 時 55 分 (EST TIME) の日付の列タイプを使用して、MySQL に値を保存しました。

bigintこれは、 value の列にUNIX 時間として保存されます。1376279700

1970 jan 01,01 00:00:00これは秒単位のエポック時間であることを理解しています。したがって、任意のタイムゾーンを使用して DateTime を初期化すると、08/11/2013 - 11:55 PM(および初期化時に使用されたタイムゾーンが何であれ)生成されるはずであると想定しました。

しかし、次のコードが与えられます:

$time1 = new DateTime("@1376279700");
$time1->setTimezone("Europe/London");
echo "Created Europe/London - ".$time1->format(DateTime::RFC822);

$time2 = new DateTime("@1376279700");
$time2->setTimezone("America/New_York");
echo "Created America/New_York - ".$time2->format(DateTime::RFC822);

私はこれらの値を取得します:

作成: ヨーロッパ/ロンドン - 月、12 8 月 13 04:55:00 +0100

作成: America/New_York - Sun, 11 Aug 13 23:55:00 -0400

タイムゾーンはEurope/London自動調整され、どういうわけか魔法のように1376279700EST タイムゾーンを使用して作成されたことを認識します。

私はここで非常に混乱しています。ここに光を当ててください。08/11/2013 11:55 PMイベントの開始日 ( ) がユーザーのタイムゾーンで使用されるタイムゾーン対応関数を作成しようとしています。

4

1 に答える 1

1

The constructor ignores the timezone when you pass a Unix timestamp.

$ts = new DateTime('@946684800');
echo $ts->format('Y-m-d H:i:s') . PHP_EOL;          // 2000-01-01 00:00:00

// Setting the time zone does nothing *here* . . .
$pac_tz = new DateTimeZone('America/Los_Angeles');
$ts = new DateTime('@946684800', $pac_tz);
echo $ts->format('Y-m-d H:i:s') . PHP_EOL;          // 2000-01-01 00:00:00

// But you can ask for the "same" timestamp 
// in a particular time zone.
$ts->setTimeZone($pac_tz);
echo $ts->format('Y-m-d H:i:s') . PHP_EOL;          // 1999-12-31 16:00:00

echo '=============' .PHP_EOL;

$time1 = new DateTime("@1376279700");
echo $time1->format('Y-m-d H:i:s').PHP_EOL;         // 2013-08-12 03:55:00

$time1_tz = new DateTimeZone("Europe/London");
$time1->setTimezone($time1_tz);
// If it's 2013-08-12 03:55:00 in UTC, what time is it in London?
// London isn't on UTC in the summer (now, as I write this). 
echo "Created Europe/London - ".$time1->format(DateTime::RFC822) . PHP_EOL;

$time2 = new DateTime("@1376279700");               // Same as $time1
echo $time2->format('Y-m-d H:i:s').PHP_EOL;

$time2_tz = new DateTimeZone("America/New_York");
// If it's 2013-08-12 03:55:00 in UTC, what time is it in New York?
$time2->setTimezone($time2_tz);
echo "Created America/New_York - ".$time2->format(DateTime::RFC822) . PHP_EOL;

All the output . . .

2000-01-01 00:00:00
2000-01-01 00:00:00
1999-12-31 16:00:00
=============
2013-08-12 03:55:00
Created Europe/London - Mon, 12 Aug 13 04:55:00 +0100
2013-08-12 03:55:00
Created America/New_York - Sun, 11 Aug 13 23:55:00 -0400
于 2013-08-27T04:05:18.067 に答える