PHP 带时区的日期时间 MongoDB\BSON\UTCDateTime
PHP DateTime with TimeZone to MongoDB\BSON\UTCDateTime
我需要有关 PHP DateTime with TimeZone 转换为 MongoDB\BSON\UTCDateTime 的帮助。
如果我有字符串 "2015-10-20T04:02:00.608000+01:00" 它会给我一个 DateTime
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', $string );
DateTime
date => "2015-10-20 04:02:00.608000"
timezone_type => 1
timezone => "+01:00"
如果我将其转换为 MongoDB\BSON\UTCDateTime 并转换回 PHP DateTime
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
$mDate->toDateTime()->setTimeZone(new DateTimeZone('Europe/Bratislava'))
我会得到一个正确的结果05:02
DateTime
date => "2015-10-20 05:02:00.000000"
timezone_type => 3
timezone => "Europe/Bratislava"
但是如果输入字符串有 +02:00 TimeZone "2015-10-20T04:02:00.608000+02:00" 并使用相同的方法,结果是
DateTime
date => "2015-10-20 04:02:00.000000"
timezone_type => 3
timezone => "Europe/Bratislava"
如果我期望 06:02,为什么第二个结果是 04:02?
回答正确。当您将 +
时区添加到时间戳时,您实际上是从 UTC 向东移动。您不是 添加 到 UTC 时间,您实际上是 减去 UTC 时间。这意味着 2015-10-20T04:02:00.608000+01:00
是世界标准时间凌晨 3 点。 2015-10-20T04:02:00.608000+02:00
是世界标准时间凌晨 2 点。如果您不断提高时区偏移量,您会更容易看到这一点
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', "2015-10-20T04:02:00.608000+01:00");
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
var_dump($date, $mDate->toDateTime());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2015-10-20 04:02:00.608000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+01:00"
}
object(DateTime)#3 (3) {
["date"]=>
string(26) "2015-10-20 03:02:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', "2015-10-20T04:02:00.608000+02:00");
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
var_dump($date, $mDate->toDateTime());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2015-10-20 04:02:00.608000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+02:00"
}
object(DateTime)#3 (3) {
["date"]=>
string(26) "2015-10-20 02:02:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
MongoDB 存储 UTC 时间戳。当您添加 Europe/Bratislava
时区时,您说的是 "What time is in Bratislava for this UTC timestamp"。 10 月(夏令时),时差 1 小时。
旁注。尝试 永不 混合 +XXXX
和 Unicode/Olson 时区 (Europe/Bratislava
)。由于夏令时,您会遇到一些非常奇怪的错误。如果您需要记录用户的本地时间以便在某个时候显示回来,请使用可选的第三个参数创建您的 DateTime
对象,例如:
$customerTz = 'Europe/Bratislava';
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.u', $dateString, $customerTz);
还要检查您是否真的需要创建一个 DateTime
,或者只需要一个新的 UTCDateTime
直接使用时间戳并在显示逻辑中处理 tz。
我需要有关 PHP DateTime with TimeZone 转换为 MongoDB\BSON\UTCDateTime 的帮助。 如果我有字符串 "2015-10-20T04:02:00.608000+01:00" 它会给我一个 DateTime
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', $string );
DateTime
date => "2015-10-20 04:02:00.608000"
timezone_type => 1
timezone => "+01:00"
如果我将其转换为 MongoDB\BSON\UTCDateTime 并转换回 PHP DateTime
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
$mDate->toDateTime()->setTimeZone(new DateTimeZone('Europe/Bratislava'))
我会得到一个正确的结果05:02
DateTime
date => "2015-10-20 05:02:00.000000"
timezone_type => 3
timezone => "Europe/Bratislava"
但是如果输入字符串有 +02:00 TimeZone "2015-10-20T04:02:00.608000+02:00" 并使用相同的方法,结果是
DateTime
date => "2015-10-20 04:02:00.000000"
timezone_type => 3
timezone => "Europe/Bratislava"
如果我期望 06:02,为什么第二个结果是 04:02?
回答正确。当您将 +
时区添加到时间戳时,您实际上是从 UTC 向东移动。您不是 添加 到 UTC 时间,您实际上是 减去 UTC 时间。这意味着 2015-10-20T04:02:00.608000+01:00
是世界标准时间凌晨 3 点。 2015-10-20T04:02:00.608000+02:00
是世界标准时间凌晨 2 点。如果您不断提高时区偏移量,您会更容易看到这一点
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', "2015-10-20T04:02:00.608000+01:00");
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
var_dump($date, $mDate->toDateTime());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2015-10-20 04:02:00.608000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+01:00"
}
object(DateTime)#3 (3) {
["date"]=>
string(26) "2015-10-20 03:02:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.uT', "2015-10-20T04:02:00.608000+02:00");
$mDate = new \MongoDB\BSON\UTCDateTime( $date->format('U') * 1000 );
var_dump($date, $mDate->toDateTime());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2015-10-20 04:02:00.608000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+02:00"
}
object(DateTime)#3 (3) {
["date"]=>
string(26) "2015-10-20 02:02:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
MongoDB 存储 UTC 时间戳。当您添加 Europe/Bratislava
时区时,您说的是 "What time is in Bratislava for this UTC timestamp"。 10 月(夏令时),时差 1 小时。
旁注。尝试 永不 混合 +XXXX
和 Unicode/Olson 时区 (Europe/Bratislava
)。由于夏令时,您会遇到一些非常奇怪的错误。如果您需要记录用户的本地时间以便在某个时候显示回来,请使用可选的第三个参数创建您的 DateTime
对象,例如:
$customerTz = 'Europe/Bratislava';
$date = DateTime::createFromFormat( 'Y-m-d\TH:i:s.u', $dateString, $customerTz);
还要检查您是否真的需要创建一个 DateTime
,或者只需要一个新的 UTCDateTime
直接使用时间戳并在显示逻辑中处理 tz。