SerializableDateTime
extends DateTime
in package
implements
Parsable, JsonSerializable
Base class for serializable DateTime objects.
Extends DateTime with Parsable interface support, enabling DateTime fields in Obj subclasses to be automatically serialized/deserialized with consistent formatting.
Subclasses must implement getFormat() to specify the DateTime format string. This pattern allows different DateTime representations in different contexts (e.g., JSONDateTime for ISO 8601, CSVDateTime for another format).
Features:
- Automatic serialization to formatted string via marshall()
- Automatic deserialization from formatted string via parse()
- JsonSerializable support
- Static factory method fromDT() for easy conversion from existing DateTime
Example:
class JSONDateTime extends SerializableDateTime {
protected static function getFormat(): string {
return 'Y-m-d\\TH:i:s.uP'; // ISO 8601 with microseconds
}
}
class Event extends Obj {
public string $title;
public JSONDateTime $date; // Automatically serialized/deserialized
}
Tags
Table of Contents
Interfaces
- Parsable
- Interface for custom serialization/deserialization of values.
- JsonSerializable
Methods
- fromDT() : static
- Create a SerializableDateTime from an existing DateTime instance.
- fromString() : static
- jsonSerialize() : string
- Serialize this DateTime for JSON encoding.
- marshall() : string
- Convert this DateTime to a formatted string.
- parse() : static
- Parse a formatted date string into this DateTime.
- getFormat() : string
- Get the DateTime format string for this class.
Methods
fromDT()
Create a SerializableDateTime from an existing DateTime instance.
public
static fromDT(DateTime $dt) : static
Convenient factory method for converting existing DateTime objects.
Parameters
- $dt : DateTime
-
The source DateTime.
Return values
static —A new instance with the same timestamp as $dt.
fromString()
public
static fromString(string $data) : static
Parameters
- $data : string
Return values
staticjsonSerialize()
Serialize this DateTime for JSON encoding.
public
jsonSerialize() : string
Implements JsonSerializable to enable json_encode() on Obj instances containing DateTime fields.
Return values
string —The formatted date/time string (via marshall()).
marshall()
Convert this DateTime to a formatted string.
public
marshall() : string
Called during serialization when this DateTime appears in an Obj field.
Return values
string —The formatted date/time string.
parse()
Parse a formatted date string into this DateTime.
public
parse(mixed $data) : static
Called during deserialization when a string is provided for a DateTime field.
Parameters
- $data : mixed
-
The date string to parse (should match getFormat()).
Tags
Return values
static —This instance, updated with the parsed timestamp.
getFormat()
Get the DateTime format string for this class.
protected
abstract static getFormat() : string
Subclasses must override this to specify how DateTime objects are formatted when serialized and expected when deserialized.
Tags
Return values
string —A format string compatible with DateTime::format() and DateTime::createFromFormat().