PHP 8.0.26 Released!

DateTimeImmutable::setDate

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

DateTimeImmutable::setDateSets the date

Description

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.

Parameters

object

Procedural style only: A DateTime object returned by date_create(). The function modifies this object.

year

Year of the date.

month

Month of the date.

day

Day of the date.

Return Values

Returns a new DateTimeImmutable object with the modified data.

Examples

Example #1 DateTimeImmutable::setDate() example

Object-oriented style

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

The above examples will output:

2001-02-03

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

The above example will output:

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

See Also

add a note

User Contributed Notes

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