DateTimeImmutable::setDate
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
DateTimeImmutable::setDate — Sets the date
說明
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.
返回值
返回新修改的 DateTimeImmutable 對像 或者在失敗時返回 false
.
範例
示例 #1 DateTimeImmutable::setDate() example
物件導向風格
<?php
$date = new DateTimeImmutable();
$newDate = $date->setDate(2001, 2, 3);
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(2001, 2, 28);
echo $newDate->format('Y-m-d') . "\n";
$newDate = $date->setDate(2001, 2, 29);
echo $newDate->format('Y-m-d') . "\n";
$newDate = $date->setDate(2001, 14, 3);
echo $newDate->format('Y-m-d') . "\n";
?>
以上例程會輸出:
2001-02-28 2001-03-01 2002-02-03
參見
- DateTimeImmutable::setISODate() - Sets the ISO date
- DateTimeImmutable::setTime() - Sets the time