2017-11-11 219 views
0

我有以下代码:碳时区功能是不正确

// $this->date_from = '11/01/2017'; 
// $this->date_to = '11/30/2017'; 

$this->where_date_from = Carbon::parse($this->date_from)->tz('America/Toronto')->startOfDay()->timestamp; 
$this->where_date_to = Carbon::parse($this->date_to)->tz('America/Toronto')->endOfDay()->timestamp; 

这产生完全innacturate时间戳。它似乎实际上是从UTC减去了两倍的偏移量。

然而,当我使用以下命令:

date_default_timezone_set('America/Toronto'); 
$this->where_date_from = strtotime($this->date_from.' 00:00:00'); 
$this->where_date_to = strtotime($this->date_to.' 23:59:59'); 

它完美。

这是怎么发生的?我想用碳来达到这个目的,所以我不必与date_default_timezone_set混淆。

回答

0

这里的问题是运行tz。事实上,它不会让你的日期在给定时区的情况下解析,但它会将日期从当前时区转换为你给出的时区。假设你有UTCtimezoneapp.php文件(什么是默认的),你现在做的:

$this->where_date_from = Carbon::parse($this->date_from, 'UTC')->tz('America/Toronto')->startOfDay()->timestamp; 

所以你承担的日期是UTC时区,然后你将它转换成美国/多伦多什么明显不是你想要的。

你应该做的分析日期时,像这样正在通过时区:

$this->where_date_from = Carbon::parse($this->date_from, 'America/Toronto')->startOfDay()->timestamp; 
$this->where_date_to = Carbon::parse($this->date_to, 'America/Toronto')->endOfDay()->timestamp;