The DateTimeImmutable class
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
简介
Representation of date and time.
This class behaves the same as DateTime except new objects are returned when modification methods such as DateTime::modify() are called.
类摘要
class DateTimeImmutable
implements
DateTimeInterface {
/* 继承的常量 */
/* 方法 */
public static createFromFormat(string
}$format
, string $datetime
, ?DateTimeZone $timezone
= null
): DateTimeImmutable|false目录
- DateTimeImmutable::add — Returns a new object, with added amount of days, months, years, hours, minutes and seconds
- DateTimeImmutable::__construct — Returns new DateTimeImmutable object
- DateTimeImmutable::createFromFormat — Parses a time string according to a specified format
- DateTimeImmutable::createFromInterface — Returns new DateTimeImmutable object encapsulating the given DateTimeInterface object
- DateTimeImmutable::createFromMutable — Returns new DateTimeImmutable object encapsulating the given DateTime object
- DateTimeImmutable::getLastErrors — Returns the warnings and errors
- DateTimeImmutable::modify — Creates a new object with modified timestamp
- DateTimeImmutable::__set_state — The __set_state handler
- DateTimeImmutable::setDate — Sets the date
- DateTimeImmutable::setISODate — Sets the ISO date
- DateTimeImmutable::setTime — Sets the time
- DateTimeImmutable::setTimestamp — Sets the date and time based on a Unix timestamp
- DateTimeImmutable::setTimezone — Sets the time zone
- DateTimeImmutable::sub — Subtracts an amount of days, months, years, hours, minutes and seconds

User Contributed Notes 3 notes
taoufik dot oussama at gmail dot com ¶
4 years ago
Here's a simple example on how this class works :
// Create a DateTimeImmutable Object
$date = new DateTimeImmutable('2000-01-01');
// "Change" that object and assign it's value to a new variable
$date2 = $date->add(new DateInterval('P6M5DT24H'));
// Check out the content of the two variables
echo $date->format('Y-m-d') . "\n";
// 2000-01-01
echo $date2->format('Y-m-d') . "\n";
// 2000-07-07
katsarov at gmail dot com ¶
3 years ago
Note: If you are trying to get 02.29 for non leap year it will return 03.01
Example:
<?php
new DateTimeImmutable('2017-02-29') // 2017-03-01
?>
soullinker0 googlemail ¶
3 years ago
class MyDateTime extends DateTimeImmutable
{
public function addDay(int $amount): MyDateTime
{
return $this->add(new DateInterval("P" . $amount . "D"));
}
}
addDay will return DateTimeImmutable not MyDateTime. It looks like there is no "return static;"
i wish i would be
备份地址:http://www.lvesu.com/blog/php/class.datetimeimmutable.php