/** * Sets the default to 0 if we have no format, or an empty string otherwise. * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); if (!isset($options['default']) and !$this->allow_null) { // Having a implies saving we're format a string, so we want a proper default $this->default = $this->format ? '' : 0; } if ($this->timezone === NULL) { $this->timezone = Jam_Timezone::instance(); } }
public static function date_span($timestamp, $local_timestamp = NULL) { $local_timestamp = $local_timestamp === NULL ? Jam_Timezone::instance()->convert(time(), Jam_Timezone::DEFAULT_TIMEZONE, Jam_Timezone::USER_TIMEZONE) : (int) $local_timestamp; // Determine the difference in seconds $offset = abs($local_timestamp - $timestamp); $timeranges = array(Date::MINUTE => array(1, 'second'), Date::HOUR => array(Date::MINUTE, 'minute'), Date::DAY => array(Date::HOUR, 'hour'), Date::WEEK * 2 => array(Date::DAY, 'day'), Date::MONTH => array(Date::WEEK, 'week'), Date::YEAR => array(Date::MONTH, 'month'), PHP_INT_MAX => array(Date::YEAR, 'year')); foreach ($timeranges as $renge => $display) { if ($offset <= $renge) { $span = floor($offset / $display[0]); $span = $span . ' ' . ($span == 1 ? $display[1] : Inflector::plural($display[1])); break; } } if ($timestamp <= $local_timestamp) { // This is in the past return $span . ' ago'; } else { // This in the future return 'in ' . $span; } }