Zend_Date 计算两个日期相减的错误
我写这个函数:
public function calcDifferentDate($dateStart, $dateEnd = false, $output = Zend_Date::DAY)
{
$dateEnd = $dateEnd ? $dateEnd : Zend_Date::now()->toString('YYYY-MM-dd');
$dateStartZD = new Zend_Date($dateStart, 'YYYY-MM-dd');
$dateEndZD = new Zend_Date($dateEnd, 'YYYY-MM-dd');
return $dateEndZD->sub($dateStartZD)->toString($output);
}
如果调用这个:
echo calcDifferentDate('2011-11-10');
今天是:'2011-11-14' 返回的输出是 05
而不是 04
为什么?我哪里做错了?
PS我使用ZF 1.11.11版本
解决方案
我找到了这个工作正确的 ! :D
public function calcDaysDiffDate($dateStart, $dateEnd = '')
{
$dateEnd = !empty($dateEnd) ? $dateEnd : Zend_Date::now()->toString('YYYY-MM-dd');
$dateStartZD = new Zend_Date($dateStart, 'YYYY-MM-dd');
$dateEndZD = new Zend_Date($dateEnd, 'YYYY-MM-dd');
$dateStartZD->sub($dateEndZD);
return $dateStartZD->getTimestamp() / (60 * 60 * 24);
}
I write this function:
public function calcDifferentDate($dateStart, $dateEnd = false, $output = Zend_Date::DAY)
{
$dateEnd = $dateEnd ? $dateEnd : Zend_Date::now()->toString('YYYY-MM-dd');
$dateStartZD = new Zend_Date($dateStart, 'YYYY-MM-dd');
$dateEndZD = new Zend_Date($dateEnd, 'YYYY-MM-dd');
return $dateEndZD->sub($dateStartZD)->toString($output);
}
If call this:
echo calcDifferentDate('2011-11-10');
and today is: '2011-11-14'
the output returned is 05
and not 04
why? where am I doing wrong?
P.S. I use ZF 1.11.11 version
I found the solution
this work right! :D
public function calcDaysDiffDate($dateStart, $dateEnd = '')
{
$dateEnd = !empty($dateEnd) ? $dateEnd : Zend_Date::now()->toString('YYYY-MM-dd');
$dateStartZD = new Zend_Date($dateStart, 'YYYY-MM-dd');
$dateEndZD = new Zend_Date($dateEnd, 'YYYY-MM-dd');
$dateStartZD->sub($dateEndZD);
return $dateStartZD->getTimestamp() / (60 * 60 * 24);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试返回这个:
计算不正确,我稍后会尝试解决这个问题。但现在,您需要您的逻辑与此类似,因为就像我在评论中所说的那样,由于以下事实,您的方法导致了
致命错误
您的日期减法返回一个整数,而不是从中调用toString()
的Zend_Date
对象。编辑
对于我之前的自以为是、没有经过深思熟虑的回答感到抱歉。经过更仔细的测试后,我相信我找到了您的问题。
sub()
函数接受可选的第二个参数$part
,它是从结果日期减法中返回的日期部分。即使可以,现在也无需调用toString()
。因此,无需进一步告别,这里是固定的返回语句:
第二次编辑
在与 OP 聊天后,由于
的差异,我的解决方案似乎不适用于 ZF 1.11.x Zend_Date::sub()
方法。Try returning this instead:
The calculations are incorrect, I will try to get to that later. But for now, you'll need your logic to be similar to that, because like I said in my comment, your method was resulting in a
fatal error
due to the fact that your date subtraction was returning an integer instead of aZend_Date
object from which to calltoString()
.Edit
Sorry about my presumptuous, not well-thought-out previous answer. After more careful testing I believe I found your issue. The
sub()
function accepts an optional second param$part
which is the part of the date will be returned from the resulting date subtraction. No need to call atoString()
now even if you could.So without further adieu, here it is with the fixed return statement:
Second Edit
After chatting with OP, it appears that my solution will not work for ZF 1.11.x due to the differences in the
Zend_Date::sub()
method.此问题的公认答案:如何比较两个 Zend_Date 对象的日期部分? 建议按以下方式使用
DateTime
而不是Zend_Date
(我对代码进行了一些修改以适合您的需求)需要):我已经尝试过了,它似乎返回了正确的结果。如果您不是绝对需要使用它,它可能是
Zend_Date
的一个很好的替代品。希望有帮助,
The accepted answer for this question: How to compare the date parts of two Zend_Date objects? recommends using
DateTime
instead ofZend_Date
in the following way (I've modified the code a bit to suit your needs):I've tried it and it seems to return the correct result. It could be a good alternative to
Zend_Date
, if you are not absolutely required to use it.Hope that helps,
我找到解决方案:
I find solution: