php / php/php-src

Wrong result from DateTimeImmutable::diff

オープン
#13,341 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Extension: date Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

If you execute this code, it produces an extremely weird result. The first DTI is 11 minutes after what we’re ->diff() ’ing against, but when in the America/Los_Angeles TZ, it produces a very strange result. Converting the first DTI to UTC then produces the expected difference of 11 minutes.

//Difference of -1 year, 11 months, 31 days, etc.
<?php
$invalid_result = (new \DateTimeImmutable('2024-01-31 17:00:00', new \DateTimeZone('America/Los_Angeles')))
    ->diff(new \DateTimeImmutable('2024-02-01 00:49:00'));

//Difference of 11 minutes
$correct_result = (new \DateTimeImmutable('2024-01-31 17:00:00', new \DateTimeZone('America/Los_Angeles')))
    ->setTimezone(new \DateTimeZone('UTC'))
    ->diff(new \DateTimeImmutable('2024-02-01 00:49:00'));

var_dump($invalid_result);
var_dump($correct_result);

$invalid_result Resulted in this output:

object(DateInterval)#3 (10) {
  ["y"]=>
  int(-1)
  ["m"]=>
  int(11)
  ["d"]=>
  int(31)
  ["h"]=>
  int(0)
  ["i"]=>
  int(11)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(0)
  ["from_string"]=>
  bool(false)
}

But I expected this output instead:

object(DateInterval)#2 (10) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(0)
  ["h"]=>
  int(0)
  ["i"]=>
  int(11)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["invert"]=>
  int(1)
  ["days"]=>
  int(0)
  ["from_string"]=>
  bool(false)
}
PHP Version

PHP 8.1

Operating System

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された PHP 8.1 スクリプトを再現し、setTimezone('UTC') ありの場合となしの場合で DateTimeImmutable::diff を比較します。DateTimeImmutable::diff のエントリポイントとタイムゾーン処理を追跡し、その後、America/Los_Angeles のケースが期待される 11 分間の間隔を生成することを示すカバレッジを追加します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。