How do you get today's date, as a date object?
作为约会对象,你如何得到今天的约会?
I'm trying to compute the difference between some start date and today. The following will not work, because getdate()
returns an array and not a date object:
我正在尝试计算一些开始日期和今天之间的差异。以下将不起作用,因为getdate()返回一个数组而不是一个日期对象:
$today = getdate();
$start = date_create('06/20/2012');
$diff = date_diff($start, $today);
echo($today . '<br/>' . $start . '<br/>' . $diff);
Output:
Array ( [seconds] => 8 [minutes] => 1 [hours] => 16 [mday] => 11 [wday] => 1 [mon] => 6 [year] => 2012 [yday] => 162 [weekday] => Monday [month] => June [0] => 1339455668 )
数组([秒] => 8 [分钟] => 1 [小时] => 16 [mday] => 11 [wday] => 1 [mon] => 6 [年] => 2012 [yday] => 162 [weekday] =>星期一[月] =>六月[0] => 1339455668)
DateTime Object ( [date] => 2012-06-20 00:00:00 [timezone_type] => 3 [timezone] => America/Los_Angeles )
DateTime对象([date] => 2012-06-20 00:00:00 [timezone_type] => 3 [timezone] => America / Los_Angeles)
3 个解决方案
#1
38
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.
操作编辑我只需要调用date_default_timezone_set,然后这段代码对我有用。
#2
11
To get difference in days use this:
为了在日子里获得好处,请使用:
$today = new DateTime('today');
the time in this object eill be 00:00:00
此对象的时间为00:00:00
If you want difference with hours minutes and seconds use this:
如果你想要小时分钟和秒钟的差异使用这个:
$now = new DateTime('now');
#3
0
I ended up using the date_create constructor (no parameter) to get the current date.
我最终使用date_create构造函数(无参数)来获取当前日期。
$diff = date_diff(date_create('06/20/2012'), date_create());
print_r($diff);
Output:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [invert] => 1 [days] => 8 )
DateInterval对象([y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [反转] => 1 [天] => 8)
I have no idea why, but Mike B's answer (and any constructor I tried for DateTime) threw an error for me in PHP5 / IIS.
我不知道为什么,但是Mike B的答案(以及我为DateTime尝试的任何构造函数)在PHP5 / IIS中为我引发了一个错误。
#1
38
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.
操作编辑我只需要调用date_default_timezone_set,然后这段代码对我有用。
#2
11
To get difference in days use this:
为了在日子里获得好处,请使用:
$today = new DateTime('today');
the time in this object eill be 00:00:00
此对象的时间为00:00:00
If you want difference with hours minutes and seconds use this:
如果你想要小时分钟和秒钟的差异使用这个:
$now = new DateTime('now');
#3
0
I ended up using the date_create constructor (no parameter) to get the current date.
我最终使用date_create构造函数(无参数)来获取当前日期。
$diff = date_diff(date_create('06/20/2012'), date_create());
print_r($diff);
Output:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [invert] => 1 [days] => 8 )
DateInterval对象([y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [反转] => 1 [天] => 8)
I have no idea why, but Mike B's answer (and any constructor I tried for DateTime) threw an error for me in PHP5 / IIS.
我不知道为什么,但是Mike B的答案(以及我为DateTime尝试的任何构造函数)在PHP5 / IIS中为我引发了一个错误。