DateTime::setDate
date_date_set
(PHP 5 >= 5.2.0, PHP 7)
DateTime::setDate -- date_date_set — Sets the date
Description
Object oriented style
public DateTime::setDate ( int $year , int $month , int $day ) : DateTime
Procedural style
date_date_set ( DateTime $object , int $year , int $month , int $day ) : DateTime
Resets the current date of the DateTime object to a different 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 the DateTime object for method chaining or false
on failure.
Examples
Example #1 DateTime::setDate() example
Object oriented style
<?php $date = new DateTime(); $date->setDate(2001, 2, 3); echo $date->format('Y-m-d'); ?>
Procedural style
<?php $date = date_create(); date_date_set($date, 2001, 2, 3); echo date_format($date, '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 DateTime(); $date->setDate(2001, 2, 28); echo $date->format('Y-m-d') . "\n"; $date->setDate(2001, 2, 29); echo $date->format('Y-m-d') . "\n"; $date->setDate(2001, 14, 3); echo $date->format('Y-m-d') . "\n"; ?>
The above example will output:
2001-02-28 2001-03-01 2002-02-03
See Also
- DateTime::setISODate() - Sets the ISO date
- DateTime::setTime() - Sets the time
© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/datetime.setdate.php