CKTIS / PHP
DateTime
Represent a mutable date and time.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
new DateTime($datetime, $timezone)Represent a mutable date and time. Methods such as modify change the same object, so another variable referring to it sees the change. Clone before editing when an independent date is needed. Specify the time zone for reproducible calendar calculations.
$date=new DateTime("2024-02-28",new DateTimeZone("UTC"));
$alias=$date; $copy=clone $date; $date->modify("+1 day");
echo json_encode([$alias->format("Y-m-d"),$copy->format("Y-m-d"),$date===$alias]);
["2024-02-29","2024-02-28",true]
Examples
<?php
$date=new DateTime("2024-02-28",new DateTimeZone("UTC"));
$alias=$date; $copy=clone $date; $date->modify("+1 day");
echo json_encode([$alias->format("Y-m-d"),$copy->format("Y-m-d"),$date===$alias]);
Result
["2024-02-29","2024-02-28",true]