PHP 8.1.28 Released!

DateTimeImmutable::setDate

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

DateTimeImmutable::setDateSets the date

Descrizione

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.

Elenco dei parametri

oggetto

Solo per lo stile procedurale: Un oggetto DateTime restituito da date_create(). La funzione modifica questo oggetto.

year

Year of the date.

month

Month of the date.

day

Day of the date.

Valori restituiti

Returns a new DateTimeImmutable object with the modified data.

Esempi

Example #1 DateTimeImmutable::setDate() example

Stile orientato agli oggetti

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

I precedenti esempi visualizzeranno:

2001-02-03

Example #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";
?>

Il precedente esempio visualizzerà:

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

Vedere anche:

add a note

User Contributed Notes

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