0
PHP的日期时间没有做什么,我希望它在这里做....麻烦与时区:PHP的DateTime返回错误的日期
class Time_model extends CI_model
{
public $time_zone;
public $tz;
public $dt;
public function __construct()
{
date_default_timezone_set('UTC');
$this->time_zone = 'Pacific/Auckland';
$this->tz = new DateTimeZone($this->time_zone);
$this->dt = new DateTime('now', $this->tz);
}
/**
* dates
*/
public function getDate()
{
$this->dt->getTimezone(); // <--- shows that the timezone is auckland where it is the 02/10/2016
return $this->dt->format('Y-m-d'); // <--- yet returns the 01/10/2016!
}
}
在奥克兰这是一个星期天,但即使我明确更改时区没有什么变化,它仍然显示为星期六。
如何获取DateTime以更改日期以匹配时区?
此外,如果将新的DateTimeZone对象传递给DateTime对象完全没有任何作用,那么传递它的第一点是什么?这真是让我烦恼,因为我知道我有一些完全错误的东西!
感谢您的澄清,所以如果您必须稍后用setTimezone()手动覆盖它,为什么要在第一个地方传入DateTimeZone? –
如果你有像'2016-10-01 14:33:21'这样的日期,并且你知道这是在'欧洲/布拉格'时区,那么你可以简单地做'new DateTime('2016-10-01 14:33: 21',新的DateTimeZone('欧洲/布拉格'))',以及它的正确。 –
只有当您需要更改时区时才需要手动覆盖 –