Arithmétique avec DateTime

Les exemples suivants montrent quelques pièges de l'arithmétique de DateTime en ce qui concerne les transitions DST et les mois ayant un nombre différent de jours.

Exemple #1 DateTimeImmutable::add/sub ajout d'un interval de temps écoulé.

Ajouter PT24H au dela d'une transition DST semblera ajouter 23/25 heures (pour la plupart des fuseaux horaires).

<?php
$dt
= new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
echo
"Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt = $dt->add(new DateInterval("PT3H"));
echo
"End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

L'exemple ci-dessus va afficher :

Start: 2015-11-01 00:00:00 -04:00
End:   2015-11-01 02:00:00 -05:00

Exemple #2 DateTimeImmutable::modify et strtotime incrémentation ou décrémentation de valeurs individuelles

Ajouter +24 heures au dela d'une transition DST peut ajouter exactement 24 heures comme vu avec la chaîne date/time (sauf si l'heure de début ou de fin est sur un point de transition).

<?php
$dt
= new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
echo
"Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt = $dt->modify("+24 hours");
echo
"End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

L'exemple ci-dessus va afficher :

Start: 2015-11-01 00:00:00 -04:00
End:   2015-11-02 00:00:00 -05:00

Exemple #3 L'ajout ou la soustraction de dates/heures peut dépasser (en plus ou en moins) des dates

Comme pour 31 Janvier + 1 mois donnera comme résultat 2 Mars (année bisextile) ou 3 Mars (année normale).

<?php
echo "Normal year:\n"; // February has 28 days
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
echo
"Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt = $dt->modify("+1 month");
echo
"End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;

echo
"Leap year:\n"; // February has 29 days
$dt = new DateTimeImmutable("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
echo
"Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt = $dt->modify("+1 month");
echo
"End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

L'exemple ci-dessus va afficher :

Normal year:
Start: 2015-01-31 00:00:00 -05:00
End:   2015-03-03 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End:   2016-03-02 00:00:00 -05:00

Pour obtenir le dernier du mois prochain (autrement dit pour prévenir le dépassement), le format last day of est disponible.

<?php
echo "Normal year:\n"; // February has 28 days
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
echo
"Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt = $dt->modify("last day of next month");
echo
"End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;

echo
"Leap year:\n"; // February has 29 days
$dt = new DateTimeImmutable("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
echo
"Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
$dt = $dt->modify("last day of next month");
echo
"End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
?>

L'exemple ci-dessus va afficher :

Normal year:
Start: 2015-01-31 00:00:00 -05:00
End:   2015-02-28 00:00:00 -05:00
Leap year:
Start: 2016-01-31 00:00:00 -05:00
End:   2016-02-29 00:00:00 -05:00

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top