php / php/php-src

Add a UTC-normalizing DateTime method for ISO 8601 output

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

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

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

説明

Update: Based on feedback, this proposal no longer focuses on adding a
format constant. The preferred direction under discussion is a method that
normalizes the represented instant to UTC and returns ISO 8601 output with
millisecond precision and a Z suffix. Check this comment : https://github.com/php/php-src/issues/23491#issuecomment-5464152132

Spoiler old proposal ## Description

PHP provides predefined date format constants such as DATE_ISO8601,
DATE_ATOM, DATE_RFC3339, and DATE_RFC3339_EXTENDED. There is no predefined
format for the UTC timestamp commonly produced by JavaScript:

1970-01-01T00:00:00.000Z

For example:

new Date(0).toISOString();
// "1970-01-01T00:00:00.000Z"

Proposal

Add a predefined format for ISO 8601 timestamps with millisecond precision.

One possible name is:

DATE_ISO8601_MILLISECONDS_UTC
DateTimeInterface::ISO8601_MILLISECONDS_UTC

with the format string:

"Y-m-d\\TH:i:s.v\\Z"

The name and API shape are open for discussion.

An alternative is an offset-preserving format:

DATE_ISO8601_MILLISECONDS
DateTimeInterface::ISO8601_MILLISECONDS

using:

"Y-m-d\\TH:i:s.vP"

Example

$date = new DateTimeImmutable('@0');

echo $date->setTimezone(new DateTimeZone('UTC'))
    ->format(DATE_ISO8601_MILLISECONDS_UTC);

// 1970-01-01T00:00:00.000Z

As with the existing date format constants, formatting does not change the
timezone of the DateTime object. The caller must convert it to UTC before
using the _UTC format.

Existing constant

DATE_RFC3339_EXTENDED already includes milliseconds, but produces an offset:

1970-01-01T00:00:00.000+00:00

This is semantically equivalent to Z for UTC, but is not byte-for-byte
compatible with JavaScript's Date.prototype.toISOString().

Related discussion

PHP issue #14593 discusses the
interpretation of the Z suffix when parsing DateTime values. That issue is
about parsing and timezone representation; this proposal is about predefined
formatting constants in ext/date.

Questions

  • Is a millisecond-precision constant useful in addition to
    DATE_RFC3339_EXTENDED?
  • Should the format preserve the object's offset, or provide the Z form for
    UTC-normalized values?
  • What name and API shape should be used?
  • Would both forms introduce unnecessary overlap with the existing constants?

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

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

はじめの一歩

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

調査の方向性

まず、既存の日付フォーマット定数と ext/date における DateTime の動作、特に DATE_RFC3339_EXTENDED と UTC 変換を確認します。リンク先のコメントを読んで、推奨されるメソッドの方向性を理解してください。API の形が合意され、メソッドがミリ秒精度で Z サフィックス付きの UTC 正規化された ISO 8601 出力を生成すれば完了です。

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

評価

技術スタック
php
領域
backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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