DateTime::__construct
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
Returns new DateTime object
Description
Like DateTimeImmutable::__construct but works with DateTime. Consider using the DateTimeImmutable and features instead.
Returns a new DateTime object.
Parameters
datetimeParameter
Enter
"now"here to obtain the current time when using thetimezoneparameter.timezoneA
DateTimeZoneobject representing the timezone ofdatetime.If
timezoneis omitted or null, the current timezone will be used.NoteThe
timezoneparameter and the current timezone are ignored when thedatetimeparameter either is a UNIX timestamp (e.g.@946684800) or specifies a timezone (e.g.2010-01-28T15:00:00+02:00).
Return Values
Returns a new DateTime instance.
Errors/Exceptions
If an invalid Date/Time string is passed, DateMalformedStringException is thrown. Previous to PHP 8.3, this was Exception.
Changelog
| Version | Description |
|---|---|
| 8.3.0 | Now throws DateMalformedStringException if an invalid string is passed, instead of Exception. |
See Also
DateTimeImmutable::__construct