/** * Retrieve the timestamp from a number of different formats. * * @param mixed value to use for timestamp retrieval */ public static function getTS($value = null) { if ($value === null) { return sfDateTimeToolkit::now(); } else { if ($value instanceof sfDate) { return $value->get(); } else { if (!is_numeric($value)) { return strtotime($value); } else { if (is_numeric($value)) { return $value; } } } } throw new sfDateTimeException(sprintf('A timestamp could not be retrieved from the value: %s', $value)); }
/** * Returns the timestamp for the most recent (previous) occurance of [month]. * * @param timestamp * @param int the month of year * @return timestamp */ public static function previousMonth($ts = null, $month = sfTime::JANUARY) { // default to now if ($ts === null) { $ts = sfDateTimeToolkit::now(); } // get offsets from january $offset1 = date('m', $ts); $offset2 = $month; // adjust if date wraps into last year $offset1 += $offset1 > $offset2 ? 0 : 12; return sfTime::subtractMonth($ts, $offset1 - $offset2); }
/** * 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); }