8288分类目录 8288分类目录 8288分类目录
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: DateTimeImmutable::__construct

来源:网络转载 浏览:37584次 时间:2024-04-03
DateTimeImmutable::createFromFormat » « DateTimeImmutable::add
  • PHP 手册
  • 函数参考
  • 日期与时间相关扩展
  • 日期/时间
  • DateTimeImmutable

DateTimeImmutable::__construct

date_create_immutable

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

DateTimeImmutable::__construct -- date_create_immutable — Returns new DateTimeImmutable object

说明

面向对象风格

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

过程化风格

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

Returns new a DateTimeImmutable object.

参数

datetime

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

Enter "now" here to obtain the current time when using the $timezone parameter.

timezone

A DateTimeZone object representing the timezone of $datetime.

If $timezone is omitted or null, the current timezone will be used.

注意:

The $timezone parameter and the current timezone are ignored when the $datetime parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

返回值

Returns a new DateTimeImmutable instance. 过程化风格在失败时返回 false

错误/异常

Emits Exception in case of an error.

更新日志

版本 说明
7.1.0 From now on microseconds are filled with actual value. Not with '00000'.

范例

示例 #1 DateTimeImmutable::__construct() example

面向对象风格

<?php
try {
    $date = new DateTimeImmutable('2000-01-01');
} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

echo $date->format('Y-m-d');
?>

过程化风格

<?php
$date = date_create('2000-01-01');
if (!$date) {
    $e = date_get_last_errors();
    foreach ($e['errors'] as $error) {
        echo "$error\n";
    }
    exit(1);
}

echo date_format($date, 'Y-m-d');
?>

以上例程会输出:

2000-01-01

示例 #2 Intricacies of DateTimeImmutable::__construct()

<?php
// Specified date/time in your computer's time zone.
$date = new DateTimeImmutable('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";

// Specified date/time in the specified time zone.
$date = new DateTimeImmutable('2000-01-01', new DateTimeImmutableZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

// Current date/time in your computer's time zone.
$date = new DateTimeImmutable();
echo $date->format('Y-m-d H:i:sP') . "\n";

// Current date/time in the specified time zone.
$date = new DateTimeImmutable(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

// Using a UNIX timestamp.  Notice the result is in the UTC time zone.
$date = new DateTimeImmutable('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";

// Non-existent values roll over.
$date = new DateTimeImmutable('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

以上例程的输出类似于:

2000-01-01 00:00:00-05:00
2000-01-01 00:00:00+12:00
2010-04-24 10:24:16-04:00
2010-04-25 02:24:16+12:00
2000-01-01 00:00:00+00:00
2000-03-01 00:00:00-05:00
add a note

User Contributed Notes 1 note

up down 2 wangbuying at gmail dot com2 years ago 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.
add a note

官方地址:https://www.php.net/manual/en/datetimeimmutable.construct.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net