/** * Gets the difference of two date values in a sfTime unit * * @param mixed timestamp, string, or sfDate object * @param int the unit to diff by (default to sfTime::SECOND) * @return int the difference in the unit * @throws sfDateTimeException */ public function diff($other, $unit = sfTime::SECOND) { // jme- modification to get a $unit $other_ts = sfDateTimeToolkit::getTS($other); $diff_ts = $this->ts - $other_ts; // determine which unit of time to add by switch ($unit) { case sfTime::SECOND: $factor = 1; break; case sfTime::MINUTE: $factor = 1 * 60; break; case sfTime::HOUR: $factor = 1 * 60 * 60; break; case sfTime::DAY: $factor = 1 * 60 * 60 * 24; break; case sfTime::WEEK: $factor = 1 * 60 * 60 * 24 * 7; break; case sfTime::MONTH: return $this->diffMonth($other); case sfTime::YEAR: return $this->diffYear($other); // -TODO to do later // -TODO to do later case sfTime::QUARTER: case sfTime::DECADE: case sfTime::CENTURY: case sfTime::MILLENIUM: default: throw new sfDateTimeException(sprintf('The unit of time provided is not valid: %s', $unit)); } // compute and return the result $diff = $diff_ts > 0 ? floor($diff_ts / $factor) : ceil($diff_ts / $factor); return $diff; }
/** * Gets the difference of two date values in seconds. * * @param mixed timestamp, string, or sfDate object * @param int the difference in seconds */ public function diff($value) { $ts = sfDateTimeToolkit::getTS($value); return $this->ts - $ts; }
/** * Gets the difference of two date values in a sfTime unit * * @param mixed timestamp, string, or sfDate object * @param int the unit to diff by (default to sfTime::SECOND) * @return int the difference in the unit * @throws sfDateTimeException */ public function diff($other, $unit = sfTime::SECOND) { // jme- modification to get a $unit $other_ts = sfDateTimeToolkit::getTS($other); $diff_ts = $this->ts - $other_ts; // determine which unit of time to add by switch ($unit) { case sfTime::SECOND: $factor = 1; break; case sfTime::MINUTE: $factor = 1 * 60; break; case sfTime::HOUR: $factor = 1 * 60 * 60; break; case sfTime::DAY: $factor = 1 * 60 * 60 * 24; break; case sfTime::WEEK: $factor = 1 * 60 * 60 * 24 * 7; break; // jme- not doing the rest due to some special cases // - TODO to do later // - e.g. how many days in a MONTH ? 28, 29, 30, 31 ? // - rought approximation ok ? NO! as it may lead to confusion // jme- not doing the rest due to some special cases // - TODO to do later // - e.g. how many days in a MONTH ? 28, 29, 30, 31 ? // - rought approximation ok ? NO! as it may lead to confusion case sfTime::MONTH: case sfTime::QUARTER: case sfTime::YEAR: case sfTime::DECADE: case sfTime::CENTURY: case sfTime::MILLENIUM: default: throw new sfDateTimeException(sprintf('The unit of time provided is not valid: %s', $unit)); } // compute and return the result return (int) (($diff_ts + $factor / 2) / $factor); }