【发布时间】:2016-12-20 15:51:59
【问题描述】:
我正在做一个支持多个国家的项目,目前是欧洲国家。
我将所有国家/地区的日期存储在带有 UTC 时区的数据库中。 当我选择时,我想显示每个国家/地区的相应时间。 例如,如果保存的日期时间是 [2016-12-20 07:00:00] UTC
所以对于德国应该是 2016-12-20 08:00:00 英国应该 2016-12-20 07:00:00
所以不要检查它是哪个国家/地区
if($country === 'DE'){
return (new DateTime($time))->setTimeZone(new DateTimeZone('Europe/Berlin'));
}else if ($country === 'UK'){
return (new DateTime($time))->setTimeZone(new DateTimeZone('Europe/London'));
}
等等
我正在考虑创建一个 DateTime 管理器来管理设置时区。
我想出了以下尝试
<?php
namespace App\Library\Values;
use App\Library\System\Locale;
final class DateTimeImmutableTimeZone extends \DateTimeImmutable
{
/**
* @var array
*/
private $timezones = [
'GB' => 'Europe/London',
'DE' => 'Europe/Berlin',
'FR' => 'Europe/Paris',
'ES' => 'Europe/Madrid',
'IT' => 'Europe/Rome',
'SE' => 'Europe/Stockholm',
'NL' => 'Europe/Amsterdam',
'BE' => 'Europe/Brussels',
'DK' => 'Europe/Copenhagen',
];
/**
* @param Locale $locale
*/
public function __construct(Locale $locale)
{
$this->setTimezone(
new \DateTimeZone($this->timezones[(string)$locale->country()])
);
}
}
但是当我尝试: var_dump(新日期时间()); //输出:
DateTimeImmutable {#1673
+"date": "2016-12-20 07:00:00"
+"timezone_type": 3
+"timezone": "UTC"
}
var_dump(new DateTimeImmutableTimeZone(new Locale())); //输出:
DateTimeImmutable {#1673
+"date": "2016-12-20 07:00:00"
+"timezone_type": 3
+"timezone": "UTC"
}
我不知道出了什么问题? 此外,我不确定这是否是具有此类 TimeZone 管理器类的编写方法
【问题讨论】:
-
据我从文档中可以看出,
country()类上没有函数country()。这是(php.net/manual/de/class.locale.php)你正在使用的本地还是你自己写的? -
@cb0 这是伪代码想象这个国家返回德国
标签: php datetime internationalization timezone locale