【发布时间】:2017-04-26 07:23:48
【问题描述】:
为什么会发生这种情况(可能是因为Carbon 扩展了DateTime 和这个http://php.net/manual/es/datetime.format.php#114366)以及如何在更改时区时从Carbon 实例中获取正确的星期?
use Carbon\Carbon;
$wet = Carbon::parse( '2017-01-02 00:47:21', 'WET' );
$cet = Carbon::parse( '2017-01-02 00:47:21', 'CET' );
$new = $cet->copy()->tz( 'WET' );
print_r( [
'cet->format(ATOM)' => $cet->format( Carbon::ATOM ), //prints: 2017-01-02T00:47:21+01:00
'cet->format(Y W)' => $cet->format( 'Y W' ), //prints: 2017 01
'wet->format(ATOM)' => $wet->format( Carbon::ATOM ), //prints: 2017-01-02T00:47:21+00:00
'wet->format(Y W)' => $wet->format( 'Y W' ), //prints: 2017 01
'new->format(ATOM)' => $new->format( Carbon::ATOM ), //prints: 2017-01-01T23:47:21+00:00
'new->format(Y W)' => $new->format( 'Y W' ), //prints: 2017 52
] );
一个更简单的案例:
print_r( [
Carbon::create( 2017, 1, 1, 0, 0, 1 )->format( 'Y W' ), //prints: 2017 52
Carbon::create( 2016, 12, 31, 23, 59, 59 )->format( 'Y W' ), //prints: 2016 52
] );
【问题讨论】:
-
嗯,那些当地时间有 1 小时的偏移量。从 00:47 切换到 前一天的 23:47。你期待什么结果?
-
原来问题出在日期,而不是时区本身。当日期更改为当年第一个星期一的前一天时,
weekOfYear字段会更改,但实际的year不会更改。我已经发布了答案。
标签: php datetime timezone php-carbon