La classe DateTimeImmutable

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

Introduction

Représentation d'une date et heure.

Cette classe se comporte de la manière identique à DateTime à l'exception que de nouveaux objets sont retournés quand des méthodes de modification telle que DateTime::modify() sont appelées.

Synopsis de la classe

class DateTimeImmutable implements DateTimeInterface {
/* Constants héritées */
public const string DateTimeInterface::ATOM = "Y-m-d\\TH:i:sP";
public const string DateTimeInterface::COOKIE = "l, d-M-Y H:i:s T";
public const string DateTimeInterface::ISO8601 = "Y-m-d\\TH:i:sO";
public const string DateTimeInterface::ISO8601_EXPANDED = "X-m-d\\TH:i:sP";
public const string DateTimeInterface::RFC822 = "D, d M y H:i:s O";
public const string DateTimeInterface::RFC850 = "l, d-M-y H:i:s T";
public const string DateTimeInterface::RFC1036 = "D, d M y H:i:s O";
public const string DateTimeInterface::RFC1123 = "D, d M Y H:i:s O";
public const string DateTimeInterface::RFC7231 = "D, d M Y H:i:s \\G\\M\\T";
public const string DateTimeInterface::RFC2822 = "D, d M Y H:i:s O";
public const string DateTimeInterface::RFC3339 = "Y-m-d\\TH:i:sP";
public const string DateTimeInterface::RFC3339_EXTENDED = "Y-m-d\\TH:i:s.vP";
public const string DateTimeInterface::RSS = "D, d M Y H:i:s O";
public const string DateTimeInterface::W3C = "Y-m-d\\TH:i:sP";
/* Méthodes */
public __construct(string $datetime = "now", ?DateTimeZone $timezone = null)
public static createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false
public static createFromMutable(DateTime $object): static
public static getLastErrors(): array|false
public static __set_state(array $array): DateTimeImmutable
public setDate(int $year, int $month, int $day): DateTimeImmutable
public setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeImmutable
public setTime(
    int $hour,
    int $minute,
    int $second = 0,
    int $microsecond = 0
): DateTimeImmutable
public setTimestamp(int $timestamp): DateTimeImmutable
public diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval
public format(string $format): string
public getOffset(): int
public getTimestamp(): int
public __wakeup(): void
}

Historique

Version Description
7.1.0 Le constructeur DateTimeImmutable inclut désormais les microsecondes actuelles dans la valeur construite. Avant cela, il initialisait toujours les microsecondes à 0.

Sommaire

add a note add a note

User Contributed Notes 3 notes

up
17
taoufik dot oussama at gmail dot com
6 years ago
Here's a simple example on how this class works :

    // Create a DateTimeImmutable Object
    $date = new DateTimeImmutable('2000-01-01');
    // "Change" that object and assign it's value to a new variable
    $date2 = $date->add(new DateInterval('P6M5DT24H'));
    // Check out the content of the two variables
    echo $date->format('Y-m-d') . "\n";
    // 2000-01-01
    echo $date2->format('Y-m-d') . "\n";
    // 2000-07-07
up
12
katsarov at gmail dot com
4 years ago
Note: If you are trying to get 02.29 for non leap year it will return 03.01
Example:
<?php
new DateTimeImmutable('2017-02-29') // 2017-03-01
?>
up
-18
soullinker0 googlemail
5 years ago
class MyDateTime extends DateTimeImmutable
{
    public function addDay(int $amount): MyDateTime
    {
        return $this->add(new DateInterval("P" . $amount . "D"));
    }
}

addDay will return DateTimeImmutable not MyDateTime. It looks like there is no "return static;"
i wish i would be
To Top