Please note that DateTime:setDate() accepts out-of-range values without warnings.
EG:
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2008-08-03 14:52:10');
$datetime->setDate(2008, 13, 12);
?>
will reset the DateTime object's date to now. The best approach is to make a wrapper to setDate() which do check for out-of-range values.
DateTime::setDate
(PHP 5 >= 5.2.0)
DateTime::setDate — 日付を設定する
説明
DateTime オブジェクトの現在の日付をリセットし、別の日付にします。
返り値
変更した DateTime を返します。
例
例1 オブジェクト指向の使用例
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2008-08-03 14:52:10');
$datetime->setDate(2008, 10, 12);
echo $datetime->format(DATE_RFC2822);
?>
例2 手続き型の使用例
<?php
date_default_timezone_set('Europe/London');
$datetime = date_create('2008-08-03 14:52:10');
date_date_set($datetime, 2008, 10, 12);
echo date_format($datetime, DATE_RFC2822);
?>
上の例の出力は以下となります。
Sun, 12 Oct 2008 14:52:10 +0100
DateTime::setDate
melicerte at mailinator dot com
12-Oct-2009 09:12
12-Oct-2009 09:12
