The DateTimeImmutable class

(PHP 5 >= 5.5.0)

Introduction

This class behaves the same as DateTime except it never modifies itself but returns a new object instead.

Class synopsis

DateTimeImmutable implements DateTimeInterface {
/* Methods */
public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
public DateTimeImmutable add ( DateInterval $interval )
public static DateTimeImmutable createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )
public static array getLastErrors ( void )
public DateTimeImmutable modify ( string $modify )
public static DateTimeImmutable __set_state ( array $array )
public DateTimeImmutable setDate ( int $year , int $month , int $day )
public DateTimeImmutable setISODate ( int $year , int $week [, int $day = 1 ] )
public DateTimeImmutable setTime ( int $hour , int $minute [, int $second = 0 ] )
public DateTimeImmutable setTimestamp ( int $unixtimestamp )
public DateTimeImmutable setTimezone ( DateTimeZone $timezone )
public DateTimeImmutable sub ( DateInterval $interval )
public DateInterval diff ( DateTimeInterface $datetime2 [, bool $absolute = false ] )
public string format ( string $format )
public int getOffset ( void )
public int getTimestamp ( void )
public DateTimeZone getTimezone ( void )
public __wakeup ( void )
}

Table of Contents

Коментарии

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
2017-12-04 12:02:27
http://php5.kiev.ua/manual/ru/class.datetimeimmutable.html
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
2019-01-24 14:27:07
http://php5.kiev.ua/manual/ru/class.datetimeimmutable.html
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
?>
2019-04-05 17:35:45
http://php5.kiev.ua/manual/ru/class.datetimeimmutable.html

    Поддержать сайт на родительском проекте КГБ