如何计算两个 Zend_Date 对象之间的差异,以月为单位 [英] how to calculate a difference between two Zend_Date objects, in months

查看:42
本文介绍了如何计算两个 Zend_Date 对象之间的差异,以月为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Zend_Date 类的对象,我想计算它们在整个日历月内的差异.. 我该怎么做?

I have two objects of Zend_Date class and I want to calculate the difference between them in full calendar months.. How can I do this?

<?php
$d1 = new Zend_Date('1 Jan 2008');    
$d2 = new Zend_Date('1 Feb 2010');
$months = $d1->sub($d2)->get(Zend_Date::MONTH);
assert($months == -25); // failure here

提前致谢!

推荐答案

如果我正确阅读了文档,则没有实现以秒/分钟/.../月/年为单位获取 2 个日期之间差异的功能.所以需要自己计算.这样的事情会做(我不知道是否需要考虑闰年,夏令时等):

If I read the docs correctly, there's no implemented functionality for getting difference between 2 dates in seconds/minutes/.../months/years. So you need to calculate it yourself. Something like this will do (I don't know if it takes leap years, DST and such into consideration):

<?php
$d1 = new Zend_Date('1 Jan 2008');    
$d2 = new Zend_Date('1 Feb 2010');
$diff = $d1->sub($d2)->toValue();
$months = floor(((($diff/60)/60)/24)/30);

这篇关于如何计算两个 Zend_Date 对象之间的差异,以月为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆