Zend Date——日差

发布于 2024-08-26 03:38:56 字数 686 浏览 6 评论 0原文

我有下面的代码行,

$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 ..我将如何得到正确的答案

I have the below line of codes

$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;  

this will return 4

But if gave

$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; 

it will return -27 .. how will i get right answer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不即不离 2024-09-02 03:38:56
$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;

返回$天;

这给出了正确的答案

$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;

return $days;

this gives the right answer

旧梦荧光笔 2024-09-02 03:38:56

我相信问题出在你的部分字符串上。请尝试使用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);

I believe the problem is in your part string. Try YYYY-MM-dd instead.

$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);
鯉魚旗 2024-09-02 03:38:56
    $cerimonia = new Zend_Date('your date here');
    $days = $cerimonia->sub(Zend_Date::now());
    $days = round($days/86400)+1;
    $cerimonia = new Zend_Date('your date here');
    $days = $cerimonia->sub(Zend_Date::now());
    $days = round($days/86400)+1;
善良天后 2024-09-02 03:38:56

为了我自己的方便功能,我扩展了 Zend_Date。我的解决方案与 Nisanth 的类似,但有一些关键区别:

  1. 在比较之前计算两天的开始时间,
  2. 使用 round() 而不是 ceil()
  3. 不要添加 < code>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);
    }
}

I've extended Zend_Date for my own convenience functions. My solution is similar to Nisanth's, with some key differences:

  1. calculate the beginning of the day for both days before comparing
  2. use round() instead of ceil()
  3. do not add 1 to the result

Example code:

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);
    }
}
热风软妹 2024-09-02 03:38:56

如果 $date 是 Zend_Date 对象,您可以使用以下命令:

if ($date->isEarlier(Zend_Date::now()->subDay(2)){
    [...]
}

或 Zend_Date 对象的其他 subXxx 函数。

If $date is a Zend_Date object you can use the following:

if ($date->isEarlier(Zend_Date::now()->subDay(2)){
    [...]
}

or the other subXxx functions of the Zend_Date object.

远山浅 2024-09-02 03:38:56

注册日期(稍后)和购买日期(之前)之间的天数

// $datePurchase instanceof Zend_Date
// $dateRegistration instanceof Zend_Date
if($datePurchase && $dateRegistration) {
   $diff = $dateRegistration->sub($datePurchase)->toValue();
   $days = ceil($diff/60/60/24)+1;
 } 

number of days between date of registration (later) and date of purchase (before)

// $datePurchase instanceof Zend_Date
// $dateRegistration instanceof Zend_Date
if($datePurchase && $dateRegistration) {
   $diff = $dateRegistration->sub($datePurchase)->toValue();
   $days = ceil($diff/60/60/24)+1;
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文