DateTimeImmutable::__construct

date_create_immutable

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

DateTimeImmutable::__construct -- date_create_immutableReturns new DateTimeImmutable object

说明

面向对象风格

public DateTimeImmutable::__construct ( string $datetime = "now" , DateTimeZone|null $timezone = null )

过程化风格

date_create_immutable ( string $datetime = "now" , DateTimeZone|null $timezone = null ) : DateTimeImmutable|false

Like DateTime::__construct() but works with DateTimeImmutable.

User Contributed Notes

wangbuying at gmail dot com 18-Nov-2019 05:01
As the type of the first parameter is string, we can assign a relative date for it.
Example:

<?php
$immutable
= new DateTimeImmutable('-1 year', new DateTimeZone('Asia/ShangHai'));
echo
$immutable->format('Y-m-d H:i:s');
?>

It looks like the parameter of function strtotime and is very helpful for us to get an immutable relative DateTime object.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有