Zend Framework

Add a function/functionset to calculate differences

Details

  • Type: New Feature New Feature
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: Zend_Date
  • Labels:
    None

Description

Add a function or multiple functions to calculate the difference between two date objects or date strings.
The returned datepart should be selectable.

f.e.:

$date->getDifference($date2, Zend_Date::MINUTES);
$date->getDifference($date2, Zend_Date::SECONDS);
and so on...

Activity

Hide
Maxence Delannoy added a comment -

My solution :

public function getDifference(Zend_Date $date, $part = Zend_Date::SECOND)
{
    $dividers = array(
        Zend_Date::SECOND => 1,
        Zend_Date::MINUTE => 60,
        Zend_Date::HOUR => 3600,
        Zend_Date::DAY => 86400
    );
    if (!isset($dividers[$part])) {
        throw new Zend_Date_Exception('Bad part value');
    }

    $diff = $this->getTimestamp() - $date->getTimestamp();
    if (self::$_options['fix_dst']) {
        $diff += ($this->get(Zend_Date::DAYLIGHT) - $date->get(Zend_Date::DAYLIGHT)) * 3600;
    }

    return $diff / $dividers[$part];
}

Note: $_options is a static property of Zend_Date, there is no method to access it from a derived class.

Show
Maxence Delannoy added a comment - My solution :
public function getDifference(Zend_Date $date, $part = Zend_Date::SECOND)
{
    $dividers = array(
        Zend_Date::SECOND => 1,
        Zend_Date::MINUTE => 60,
        Zend_Date::HOUR => 3600,
        Zend_Date::DAY => 86400
    );
    if (!isset($dividers[$part])) {
        throw new Zend_Date_Exception('Bad part value');
    }

    $diff = $this->getTimestamp() - $date->getTimestamp();
    if (self::$_options['fix_dst']) {
        $diff += ($this->get(Zend_Date::DAYLIGHT) - $date->get(Zend_Date::DAYLIGHT)) * 3600;
    }

    return $diff / $dividers[$part];
}
Note: $_options is a static property of Zend_Date, there is no method to access it from a derived class.
Hide
Konstantin.Myakshin added a comment -

what about to implement this method in next minior release?

Show
Konstantin.Myakshin added a comment - what about to implement this method in next minior release?
Hide
Jorge Padron added a comment -

+1

Show
Jorge Padron added a comment - +1

People

Vote (9)
Watch (8)

Dates

  • Created:
    Updated: