単に次のように:
$end = new \DateTime();
$start = clone $end;
// Remove 30 days from today and set midnight
$start->sub(new \DateInterval('P30D'));
$start->setTime(0, 0);
var_dump(new \DateInterval('P30D'));
var_dump($start, $end); die();
出力 (きれいにフォーマットされています):
object(DateInterval)#1516 (8)
{ ["y"]=> int(0)
["m"]=> int(0)
["d"]=> int(30)
["h"]=> int(0)
["i"]=> int(0)
["s"]=> int(0)
["invert"]=> int(0)
["days"]=> int(0) }
object(DateTime)#1523 (3)
{ ["date"]=> string(19) "2012-06-19 00:00:00"
["timezone_type"]=> int(3)
["timezone"]=> string(11) "Europe/Rome"
}
object(DateTime)#4682 (3)
{ ["date"]=> string(19) "2012-08-18 02:42:23"
["timezone_type"]=> int(3)
["timezone"]=> string(11) "Europe/Rome"
}
1ヶ月の誤差…手がかりは?
編集:使用する場合にのみ正しい動作:
$start->modify('-30 days');
行をコメントアウトしDateInterval
ます。
PHP 5.3.2-1ubuntu4.17 with Suhosin-Patch (cli) (built: Jun 19 2012 01:35:33)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with XCache v1.3.0, Copyright (c) 2005-2009, by mOo
Server version: Apache/2.2.14 (Ubuntu)
Server built: Feb 14 2012 18:09:18