略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DateTimeImmutable::setDate

2024-05-04

DateTimeImmutable::setDate

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

DateTimeImmutable::setDateSets the date

说明

public DateTimeImmutable::setDate(int $year, int $month, int $day): DateTimeImmutable

Returns a new DateTimeImmutable object with the current date of the DateTimeImmutable object set to the given date.

参数

object

仅过程化风格:由 date_create() 返回的 DateTime 类型的对象。此函数会修改这个对象。

year

Year of the date.

month

Month of the date.

day

Day of the date.

返回值

Returns a new modified DateTimeImmutable object 或者在失败时返回 false.

范例

示例 #1 DateTimeImmutable::setDate() example

面向对象风格

<?php
$date 
= new DateTimeImmutable();
$newDate $date->setDate(200123);
echo 
$newDate->format('Y-m-d');
?>

以上例程会输出:

2001-02-03

示例 #2 Values exceeding ranges are added to their parent values

<?php
$date 
= new DateTimeImmutable();

$newDate $date->setDate(2001228);
echo 
$newDate->format('Y-m-d') . "\n";

$newDate $date->setDate(2001229);
echo 
$newDate->format('Y-m-d') . "\n";

$newDate $date->setDate(2001143);
echo 
$newDate->format('Y-m-d') . "\n";
?>

以上例程会输出:

2001-02-28
2001-03-01
2002-02-03

参见

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

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

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3