The DateTime class
[PHP 5 >= 5.2.0, PHP 7, PHP 8]
Introduction
Representation of date and time.
This class behaves the same as DateTimeImmutable except objects are modified itself when modification methods such as DateTime::modify[] are called.
Warning
Calling methods on objects of the class DateTime will change the information encapsulated in these objects, if you want to prevent that you will have to use clone
operator to create a new object. Use
DateTimeImmutable instead of DateTime to obtain this recommended behaviour by default.
Class synopsis
public setTime[
int $hour
,
int
$minute
,
int $second
= 0,
int $microsecond
= 0
]: DateTime
public format[string $format
]: string
}
Changelog
7.2.0 | The class constants of DateTime are now defined on DateTimeInterface. |
7.1.0 | The DateTime constructor now includes the current microseconds in the constructed value. Before this, it would always initialise the microseconds to 0 .
|
Table of Contents
- DateTime::add — Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds
- DateTime::__construct — Returns new DateTime object
- DateTime::createFromFormat — Parses a time string according to a specified format
- DateTime::createFromImmutable — Returns new DateTime object encapsulating the given DateTimeImmutable object
- DateTime::createFromInterface — Returns new DateTime object encapsulating the given DateTimeInterface object
- DateTime::getLastErrors — Returns the warnings and errors
- DateTime::modify — Alters the timestamp
- DateTime::__set_state — The __set_state handler
- DateTime::setDate — Sets the date
- DateTime::setISODate — Sets the ISO date
- DateTime::setTime — Sets the time
- DateTime::setTimestamp — Sets the date and time based on an Unix timestamp
- DateTime::setTimezone — Sets the time zone for the DateTime object
- DateTime::sub — Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
There are no user contributed notes for this page.
date_modify
[PHP 5 >= 5.2.0, PHP 7, PHP 8]
DateTime::modify -- date_modify — Alters the timestamp
Description
Object-oriented style
public DateTime::modify[string $modifier
]:
DateTime|false
Return Values
Returns the modified DateTime object for method chaining or false
on failure.
Examples
Example #1 DateTime::modify[] example
Object-oriented style
Procedural style
The above examples will output:
Example #2 Beware when adding or subtracting months
The above example will output:
See Also
- strtotime[] - Parse about any English textual datetime description into a Unix timestamp
- DateTimeImmutable::modify[] - Creates a new object with modified timestamp
- DateTime::add[] - Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds
- DateTime::sub[] - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
- DateTime::setDate[] - Sets the date
- DateTime::setISODate[] - Sets the ISO date
- DateTime::setTime[] - Sets the time
- DateTime::setTimestamp[] - Sets the date and time based on an Unix timestamp
There are no user contributed notes for this page.