Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Date -- day difference

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

like image 395
Nisanth Kumar Avatar asked Mar 25 '10 07:03

Nisanth Kumar


2 Answers

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.

like image 100
obotezat Avatar answered Oct 27 '22 20:10

obotezat


$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

like image 21
Nisanth Kumar Avatar answered Oct 27 '22 20:10

Nisanth Kumar