DateTime::setDate

date_date_set

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

DateTime::setDate -- date_date_setEstablece la fecha

Descripción

Estilo orientado a objetos

public DateTime::setDate(int $year, int $month, int $day): DateTime

Estilo por procedimientos

date_date_set(
    DateTime $object,
    int $year,
    int $month,
    int $day
): DateTime

Reinicia la fecha actual del objeto DateTime a una fecha diferente.

Parámetros

object

Solamente para el estilo por procedimientos: Un objeto DateTime devuelto por date_create(). La función modifica este objeto.

year

Año de la fecha.

month

Mes de la fecha.

day

Día de la fecha.

Valores devueltos

Devuelve el objeto DateTime para la cadena de métodos o false en caso de error.

Historial de cambios

Versión Descripción
5.3.0Se ha cambiado el valor devuelto en caso de éxito de null a DateTime.

Ejemplos

Ejemplo #1 Ejemplo de DateTime::setDate()

Estilo orientado a objetos

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

Estilo por procedimientos

<?php
$fecha
= date_create();
date_date_set($fecha, 2001, 2, 3);
echo
date_format($fecha, 'Y-m-d');
?>

El resultado de los ejemplos sería:

2001-02-03

Ejemplo #2 Los valores que exceden rangos son añadidos a sus valores padre

<?php
$fecha
= new DateTime();

$fecha->setDate(2001, 2, 28);
echo
$fecha->format('Y-m-d') . "\n";

$fecha->setDate(2001, 2, 29);
echo
$fecha->format('Y-m-d') . "\n";

$fecha->setDate(2001, 14, 3);
echo
$fecha->format('Y-m-d') . "\n";
?>

El resultado del ejemplo sería:

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

Ver también

add a note add a note

User Contributed Notes 3 notes

up
8
kevin dot nadin at gmail dot com
6 years ago
Be carreful about this bug in php 5.6 and lower (fixed in php 7.0 and higher) :

<?php
$date
= new DateTime("first day of last month");
echo
$date->format('Y-m-d');
echo 
' => ' ;
$date->setDate(2013, 2, 3);
echo
$date->format('Y-m-d');
?>

Output <=5.6 : 2017-03-01 => 2013-02-01
Output >=7.0 : 2017-03-31 => 2013-02-03

Same goes for "Last day of last month", and the funny part, it will take the last day of the new month setted by setDate

Example with a Leap Year :
<?php
$date
= new DateTime("last day of last month");
echo
$date->format('Y-m-d');
echo 
' => ' ;
$date->setDate(2012, 2, 3);
echo
$date->format('Y-m-d');
?>

Output <=5.6 : 2017-03-31 => 2012-02-29
Output >=7.0 : 2017-03-31 => 2012-02-03
up
3
remy215 at laposte dot net
12 years ago
Be warned, DateTime::setDate() does not check for invalid input.

Illustration:
<?php
$dt
= new DateTime();
$dt->setDate(2012, 11, 31); // returns DateTime object and not false although this date does not exist
echo $dt->format('Y-m-d'); // output: 2012-12-01
?>

No error was generated on entering a non existing date, php silently changed it.
up
-2
kevin dot nadin at gmail dot com
6 years ago
Correction on the previous comment :

Bug fixed in php 7.0.17 and 7.1.3, for the version 7.0.0 to 7.0.16 and 7.1.0 to 7.1.2, the bug is still present
To Top