我有以下代码行
$day1 = new Zend_Date('2010-03-01', 'YYYY-mm-dd');
$day2 = new Zend_Date('2010-03-05', 'YYYY-mm-dd');
$dateDiff = $day2->getDate()->get(Zend_Date::TIMESTAMP) - $day1->getDate()->get(Zend_Date::TIMESTAMP);
$days = floor((($dateDiff / 60) / 60) / 24);
return $days;
这将返回4
但是如果给了
$day1 = new Zend_Date('2010-02-28', 'YYYY-mm-dd');
$day2 = new Zend_Date('2010-03-01', 'YYYY-mm-dd');
$dateDiff = $day2->getDate()->get(Zend_Date::TIMESTAMP) - $day1->getDate()->get(Zend_Date::TIMESTAMP);
$days = floor((($dateDiff / 60) / 60) / 24);
return $days;
它将返回-27 ..我将如何得到正确答案
答案 0 :(得分:15)
$firstDay = new Zend_Date('2010-02-28', 'YYYY-MM-dd');
$lastDay = new Zend_Date('2010-03-01', 'YYYY-MM-dd');
$diff = $lastDay->sub($firstDay)->toValue();
$days = ceil($diff/60/60/24) +1;
返回$ days;
这给出了正确的答案
答案 1 :(得分:7)
我相信问题出现在你的部分字符串中。请改为 YYYY-MM-dd 。
$day1 = new Zend_Date('2010-02-28', 'YYYY-MM-dd');
$day2 = new Zend_Date('2010-03-01', 'YYYY-MM-dd');
echo $day2->sub($day1)->toString(Zend_Date::DAY);
答案 2 :(得分:3)
$cerimonia = new Zend_Date('your date here');
$days = $cerimonia->sub(Zend_Date::now());
$days = round($days/86400)+1;
答案 3 :(得分:2)
我为自己的便利功能扩展了Zend_Date
。我的解决方案类似于Nisanth,有一些关键的区别:
round()
代替ceil()
1
添加到结果示例代码:
class My_Date extends Zend_Date
{
public static function now($locale = null)
{
return new My_Date(time(), self::TIMESTAMP, $locale);
}
/**
* set to the first second of current day
*/
public function setDayStart()
{
return $this->setHour(0)->setMinute(0)->setSecond(0);
}
/**
* get the first second of current day
*/
public function getDayStart()
{
$clone = clone $this;
return $clone->setDayStart();
}
/**
* get count of days between dates, ignores time values
*/
public function getDaysBetween($date)
{
// 86400 seconds/day = 24 hours/day * 60 minutes/hour * 60 seconds/minute
// rounding takes care of time changes
return round($date->getDayStart()->sub(
$this->getDayStart()
)->toValue() / 86400);
}
}
答案 4 :(得分:1)
如果$ date是Zend_Date对象,您可以使用以下内容:
if ($date->isEarlier(Zend_Date::now()->subDay(2)){
[...]
}
或Zend_Date对象的其他subXxx函数。
答案 5 :(得分:0)
注册日期(之后)与购买日期之间(之前)的天数
// $datePurchase instanceof Zend_Date
// $dateRegistration instanceof Zend_Date
if($datePurchase && $dateRegistration) {
$diff = $dateRegistration->sub($datePurchase)->toValue();
$days = ceil($diff/60/60/24)+1;
}