Date validity can be checked with a custom class.
DateTime::setDate
date_date_set
(PHP 5 >= 5.2.0)
DateTime::setDate -- date_date_set — Sets the date
Opis
Styl obiektowy
Styl proceduralny
Resets the current date of the DateTime object to a different date.
Parametry
-
object -
Tylko styl proceduralny: Obiekt DateTime zwracany przez date_create(). Funkcja modyfikuje ten obiekt.
-
year -
Year of the date.
-
month -
Month of the date.
-
day -
Day of the date.
Zwracane wartości
Zwraca zmodyfikowany obiekt DateTime lub FALSE w przypadku niepowodzenia.
Rejestr zmian
| Wersja | Opis |
|---|---|
| 5.3.0 | Zmieniono zwracaną
wartość w przypadku powodzenia z NULL na DateTime. |
Przykłady
Przykład #1 DateTime::setDate() example
Styl obiektowy
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
Styl proceduralny
<?php
$date = date_create();
date_date_set($date, 2001, 2, 3);
echo date_format($date, 'Y-m-d');
?>
Powyższe przykłady wyświetlą:
2001-02-03
Przykład #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";
?>
Powyższy przykład wyświetli:
2001-02-28 2001-03-01 2002-02-03
Zobacz też:
- DateTime::setISODate() - Sets the ISO date
- DateTime::setTime() - Sets the time
wkeevers96 at yahoo dot com
19-Mar-2012 05:44
remy215 at laposte dot net
11-Jan-2012 10:59
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.
