The 5th Annual China PHP Conference

DateTime::setDate

date_date_set

(PHP 5 >= 5.2.0, PHP 7)

DateTime::setDate -- date_date_setSets the date

Descrizione

Stile orientato agli oggetti

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

Stile procedurale

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

Resets the current date of the DateTime object to a different 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

Restituisce l'oggetto DateTime per il metodo chaining o FALSE in caso di fallimento.

Log delle modifiche

Versione Descrizione
5.3.0Modificato il valore di ritorno in caso di successo da NULL a DateTime.

Esempi

Example #1 DateTime::setDate() example

Stile orientato agli oggetti

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

Stile procedurale

<?php
$date 
date_create();
date_date_set($date200123);
echo 
date_format($date'Y-m-d');
?>

I precedenti esempi visualizzeranno:

2001-02-03

Example #2 Values exceeding ranges are added to their parent values

<?php
$date 
= new DateTime();

$date->setDate(2001228);
echo 
$date->format('Y-m-d') . "\n";

$date->setDate(2001229);
echo 
$date->format('Y-m-d') . "\n";

$date->setDate(2001143);
echo 
$date->format('Y-m-d') . "\n";
?>

Il precedente esempio visualizzerĂ :

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

Vedere anche:

add a note add a note

User Contributed Notes 5 notes

up
4
indjeto at mail dot bg
2 months ago
I found a bug:
$date = new \DateTime('first day of this month');
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');

This outputs: 2001-02-01
The day never changes.
up
6
remy215 at laposte dot net
5 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
0
rick at sublymesystems dot com
1 month ago
Indjeto: You creating the datetime object with the first day of the month you are currently in when you initialize the object. Then you reset the date to the date you entered. Then you output, which is the day you entered because thats what you set it to. If you want to see the result you are assumed to be expecting you should not run the setDate() method.
up
0
kevin dot nadin at gmail dot com
1 month 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
up
0
kevin dot nadin at gmail dot com
1 month 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
To Top