3

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);
}
JellyBelly
  • 2,451
  • 2
  • 21
  • 41
  • I got `Fatal error: Call to a member function toString() on a non-object in ...` and `$dateEndZD->sub($dateStartZD)` is returning 345600. – Yes Barry Nov 14 '11 at 11:05
  • what is this function supposed to do? I looked at it for 30s now and it doesnt make any sense to me. – Gordon Nov 14 '11 at 12:47
  • My function is used to calculate the difference in days between two dates, it is not clear? :S – JellyBelly Nov 14 '11 at 14:39

3 Answers3

1

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.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
  • -1 have you tried the code before posting? with your method returns 5, not 4! :S – JellyBelly Nov 14 '11 at 11:18
  • I kinda did but I'm half asleep. it's almost sunrise here and I haven't gone to sleep yet. sorry for my half answer. If you haven't found your solution by tomorrow I will definitely give this another try and this time I'll be working with full brain power. – Yes Barry Nov 14 '11 at 11:21
  • Ok! Thanks anyway! Good night! – JellyBelly Nov 14 '11 at 11:22
  • Ok nevermind, I couldn't leave it like that. I think I found the solution. Try it now. – Yes Barry Nov 14 '11 at 11:31
  • Correction: I _know_ I found the solution. I tested that function out with about 5 different dates and they all worked. – Yes Barry Nov 14 '11 at 11:39
  • sorry but your solution return a Zend_Date object and not number day! :S – JellyBelly Nov 14 '11 at 11:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4971/discussion-between-jellybelly-and-mmmshuddup) – JellyBelly Nov 14 '11 at 11:42
1

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,

Community
  • 1
  • 1
dinopmi
  • 2,683
  • 19
  • 24
  • yours is a good workaround, but I am always curious as to why the solution with Zend_Date is not behaving as it should. – JellyBelly Nov 14 '11 at 15:39
  • After a second viewing of your solution is not what I needed! Why not have your way with the sign of the difference. Your method returns the simple difference and not -1 or 1, which is my case! :S – JellyBelly Nov 14 '11 at 15:49
  • I'm not sure I understood your comment... In your comment to your original question you said that the function "is used to calculate the difference in days between two dates", right? – dinopmi Nov 14 '11 at 16:04
  • Yes, you're absolutely right, but when I `2011-11-10` and `2011-11-14` I must return to `-4` and not `4`! – JellyBelly Nov 14 '11 at 16:17
  • I for different I mean subtraction! – JellyBelly Nov 14 '11 at 16:19
  • Ok, I understand now. In that case, you could combine a call to diff with comparison operators, available for DateTime objects according to the documentation: http://au.php.net/manual/en/datetime.diff.php – dinopmi Nov 14 '11 at 16:20
1

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);
}
JellyBelly
  • 2,451
  • 2
  • 21
  • 41