大网数据广告活动
PHP 语言与运行手册

DateTime::__construct · 返回新的 DateTime 对象

· 52CE 我爱测

跟 DateTimeImmutable::__construct() 一样,但适用于 DateTime。Consider using the DateTimeImmutable and features instead.

DateTime::__construct

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

DateTime::__construct — 返回新的 DateTime 对象

说明

public function DateTime::__construct(string $datetime = "now", ?DateTimeZone $timezone = null)

跟 DateTimeImmutable::__construct() 一样,但适用于 DateTime。Consider using the DateTimeImmutable and features instead.

返回新的 DateTime 对象。

参数

datetime

日期/时间字符串。正确格式的说明详见 日期与时间格式。

如果这个参数为字符串 "now" 表示获取当前时间。 如果同时指定了 $timezone 参数,那么获取指定时区的当前时间。

timezone

DateTimeZone 对象表示 $datetime 的时区。

如果省略了 $timezone 参数或者传递 null, 那么会使用当前时区。

注意:

当 $datetime 参数是 UNIX 时间戳(例如 @946684800),或者已经包含时区信息(例如 2010-01-28T15:00:00+02:00)的时候,$timezone 参数和当前时区都将被忽略。

返回值

返回一个新的 DateTime 对象实例,或者在发生错误的时候返回

错误/异常

如果传递无效的日期/时间字符串,将会抛出 DateMalformedStringException,在 PHP 8.3 之前,将抛出 Exception。

更新日志

版本 说明
8.3.0 传递无效的字符串现在抛出 DateMalformedStringException 而不是 Exception。

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。