Zend Date -- day difference
Asked Answered
G

6

9

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

Garrido answered 25/3, 2010 at 7:57 Comment(0)
G
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;

return $days;

this gives the right answer

Garrido answered 26/3, 2010 at 8:45 Comment(0)
F
8

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);
Filmore answered 25/3, 2010 at 9:1 Comment(5)
Beware 'YYYY' is the ISO Year. Use 'yyyy' for the year.Harborage
Zend Framework Date::sub() returns a difference in seconds and not an object toString won't work.Enphytotic
Looking at the date code right now and sub() returns $this at the end (which is a Zend_Date object). This is with version 1.11 of the zend framework.Filmore
This code will not work as expected if the difference is bigger than one monthDearth
This approach will modify the day2 Object, which may not be desired, considering OP only asked for a comparison in days. I, for one, would want to avoid modifying Objects as they may be needed with the original date later on.Bolick
G
3
    $cerimonia = new Zend_Date('your date here');
    $days = $cerimonia->sub(Zend_Date::now());
    $days = round($days/86400)+1;
Gaussmeter answered 10/8, 2010 at 15:19 Comment(0)
O
2

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);
    }
}
Ona answered 25/6, 2010 at 13:43 Comment(0)
B
2

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.

Butte answered 11/1, 2013 at 10:40 Comment(1)
Loved your approach. I used with ->isLater, that is also available (for other purpose, of course).Lesalesak
R
0

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;
 } 
Recollect answered 23/3, 2011 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.