Dutch PHP Conference 2025 - Call For Papers

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

Тільки процедурний стиль: об'єкт DateTime повертається функцією date_create(). Вона змінює цей об'єкт.

year

Year of the date.

month

Month of the date.

day

Day of the date.

Значення, що повертаються

Повертає новий об'єкт DateTimeImmutable зі зміненими даними.

Приклади

Приклад #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

Прогляньте також

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top