Bug with Zend_Date calculating subtraction two date

1.7k Views Asked by At

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);
}
3

There are 3 best solutions below

0
On BEST ANSWER

I find solution:

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);
}
7
On

Try returning this instead:

$newDate = new Zend_Date($dateEndZD->sub($dateStartZD), 'YYYY-MM-dd');
return $newDate->get($output);

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 a Zend_Date object from which to call toString().

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 a toString() now even if you could.

So without further adieu, here it is with the fixed return statement:

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, $output); // <-- fixed
}

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.

6
On

The accepted answer for this question: How to compare the date parts of two Zend_Date objects? recommends using DateTime instead of Zend_Date in the following way (I've modified the code a bit to suit your needs):

$date1 = new DateTime('2011-11-14');
$date2 = new DateTime('2011-11-10');
$diffDays = $date1->diff($date2)->days;

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,