Add a UTC-normalizing DateTime method for ISO 8601 output
Personne n'a encore pris cette issue.
- Langage dominant
- C
- Étoiles
- 40.4k
- Forks
- 8.1k
- Merge moyen
- 2 j 13 h
- PR mergées (30 j)
- 96
Description
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 aZsuffix. Check this comment : https://github.com/php/php-src/issues/23491#issuecomment-5464152132
Spoiler old proposal
## DescriptionPHP 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
Zform for
UTC-normalized values? - What name and API shape should be used?
- Would both forms introduce unnecessary overlap with the existing constants?
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner les constantes existantes de formatage des dates et le comportement de DateTime dans ext/date, en particulier DATE_RFC3339_EXTENDED et la conversion UTC. Lisez le commentaire lié pour comprendre l’orientation préférée de la méthode ; c’est terminé lorsque la forme de l’API est convenue et que la méthode produit une sortie ISO 8601 normalisée en UTC, avec une précision à la milliseconde et un suffixe Z.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- php
- Domaine
- backend
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- Active
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100