DateTime::__serialize

DateTimeImmutable::__serialize

DateTimeInterface::__serialize

(PHP 8 >= 8.2.0)

DateTime::__serialize -- DateTimeImmutable::__serialize -- DateTimeInterface::__serializeDeserializa un DateTime

Descripción

public function DateTime::__serialize(): array
public function DateTimeImmutable::__serialize(): array
public function DateTimeInterface::__serialize(): array

El gestor __serialize().

Parámetros

Esta función no contiene ningún parámetro.

Valores devueltos

La representación serializada del objeto DateTime.

Ejemplos

Ejemplo #1 Ejemplo de DateTime::__serialize()

<?php

class CustomDateTimeImmutable extends DateTimeImmutable
{
    #[\Override]
    public function __serialize(): array
    {
        return [
            'date' => $this->format(DateTimeInterface::W3C),
            'timestamp' => $this->getTimestamp(),
            'timezone' => $this->getTimeZone()->getName(),
            'to' => 'Drink a cup of coffee',
        ];
    }
}

$date = new CustomDateTimeImmutable('2025-03-27');
var_dump(serialize($date));

El ejemplo anterior mostrará:

string(171) "O:23:"CustomDateTimeImmutable":4:{s:4:"date";s:25:"2025-03-27T00:00:00+00:00";s:9:"timestamp";i:1743033600;s:8:"timezone";s:3:"UTC";s:2:"to";s:21:"Drink a cup of coffee";}"

Notas

Advertencia

Se lanza un Error al intentar deserializar un objeto DateTime personalizado si __serialize() está definido pero __unserialize() está ausente.

Véase también

add a note

User Contributed Notes

There are no user contributed notes for this page.