你如何获得今天的日期,日期对象?
我想计算一些开始日期和今天之间的差异。 下面将无法正常工作,因为getdate()
返回一个数组,而不是一个对象日期:
$today = getdate();
$start = date_create('06/20/2012');
$diff = date_diff($start, $today);
echo($today . '<br/>' . $start . '<br/>' . $diff);
输出:
阵列([秒] => 8 [分钟] => 1 [小时] => 16 [MDAY] => 11 [wday] => 1 [问] => 6年] => 2012 [yday] => 162 [平日] =>周一[一个月] => 6月。[0] => 1339455668)
DateTime对象([日期] => 2012-06-20 00:00:00 [timezone_type] => 3 [时区] =>美国/洛杉矶)
new DateTime('now');
http://www.php.net/manual/en/datetime.construct.php
Comparing is easy:
$today = new DateTime('now');
$newYear = new DateTime('2012-01-01');
if ($today > $newYear) {
}
Op's edit
I just needed to call date_default_timezone_set, and then this code worked for me.
要在天使用该得到的区别:
$today = new DateTime('today');
在此对象中的时间是奥尼尔00:00:00
如果你想与小时分差秒使用:
$now = new DateTime('now');
我结束了使用构造函数(无参数)来得到当前的日期。
$diff = date_diff(date_create('06/20/2012'), date_create());
print_r($diff);
输出:
DateInterval对象([Y] => 0 [M] => 0 [d] => 8并[h] => 6 [I] => 30 [秒] => 40 [反相] => 1 [天] => 8)
我不知道为什么,但我是迈克B的答案(我试过的DateTime任何构造函数)在PHP5 / IIS抛出错误我。