2017-11-11 9 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입니다. 사실 그것은 당신의 날짜가 주어진 시간대를 가정 할 때 해석되지는 않을 것이지만, 그것은 현재 시간대에서 당신이 준 시간대로 날짜를 변환 할 것입니다. 당신이 (기본이 무엇인지) app.php 파일에 UTC에서 timezone 세트가 가정, 당신이 지금하고 있습니다 :

$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;