The DateTimeImmutable class

(PHP 5 >= 5.5.0, PHP 7)

简介

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

类摘要

DateTimeImmutable implements DateTimeInterface {
/* 方法 */
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 DateTimeImmutable createFromMutable ( DateTime $datetime )
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 [, int $microseconds = 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

User Contributed Notes

samundra dot shr at gmail dot com 25-Jan-2015 01:46
Simple usage of DateTimeImmutable Class:

<?php
$format
= "Y-m-d";
$dateString = "2015-01-25";

// Get new DateTimeImmutable Object
$date = DateTimeImmutable::createFromFormat($format,$dateString);

var_dump($date);

// DateTimeImmutable object can be used similar to DateTime object
echo $date->format("Y/m/d");