Php DateTime::diff year不工作
我不明白为什么Php DateTime::diff year不工作,php,datetime,Php,Datetime,我不明白为什么DateTime::diff的年份(y)不起作用 这是我的 $startDate = new DateTime('2009-12-01'); $endDate = new DateTime('2014-12-01'); // $current = $workid->current; if($current) // currently works at this job { // $endDate = new
DateTime::diff
的年份(y)不起作用
这是我的
$startDate = new DateTime('2009-12-01');
$endDate = new DateTime('2014-12-01');
// $current = $workid->current;
if($current) // currently works at this job
{
// $endDate = new DateTime(); // current date/time
}
$diff = $endDate->diff($startDate);
if($diff->y = 0)
{
if($diff->m > 1)
{
$string = $diff->m . ' months';
}
else
{
$string = '1 month';
}
}
elseif ($diff->y = 1)
{
if($diff->m > 1)
{
$string = '1 year ' . $diff->m . ' months';
}
else
{
$string = '1 year 1 month';
}
}
else
{
if($diff->m > 1)
{
$string = $diff->y . ' years ' . $diff->m . ' months';
}
else
{
$string = $diff->y . ' years 1 month';
}
}
print_r($diff);
它输出
DateInterval Object ( [y] => 1 [m] => 0 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 1 [days] => 1826 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
[y]
应该说5
,但它说1
,无论我将$startDate改为年份
这是我的php-v
输出
PHP 5.5.9-1ubuntu4.5 (cli) (built: Oct 29 2014 11:59:10)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
将您的条件中的=
更改为=
(全部)
否则,您进行赋值,最后一个赋值是$diff->y=1
,因此输出为1
您必须将其更改为:$diff->y==1
,以便与此值进行比较
有关比较运算符的更多信息,请参阅:如果这有什么不同,我将使用Laravel。不应该这样做,因为您没有使用该框架的代码。这里是您脚本的全部代码吗?或者你有其他可以改变输出的代码吗?@Rizier123我更新了整个代码。介于$diff=$endDate->diff($startDate)之间的内容代码>和打印($diff)代码>正在把[y]值搞砸。该死。我还在学习。:)我想对于实数(整数)我只需要一个=
,对于“字符串”之类的东西我只需要两个=
。谢谢