/** * Automatically creates or updates the time and * converts it, if necessary. * * @param Jam_Model $model * @param mixed $value * @param boolean $is_loaded * @return int|string */ public function convert(Jam_Validated $model, $value, $is_loaded) { // Do we need to provide a default since we're creating or updating if (!$is_loaded and $this->auto_now_create or $is_loaded and $this->auto_now_update) { $value = $this->timezone !== FALSE ? $this->timezone->time() : time(); } else { // Convert to UNIX timestamp if (is_numeric($value)) { $value = (int) $value; } elseif (FALSE !== ($to_time = strtotime($value))) { $value = $to_time; } if (is_numeric($value) and $value and $this->timezone !== FALSE) { $value = $this->timezone->convert($value, Jam_Timezone::USER_TIMEZONE, Jam_Timezone::MASTER_TIMEZONE); } } // Convert if necessary if ($this->format and is_numeric($value)) { $value = date($this->format, $value); } return $value; }
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; } }
public function test_date() { $unchanged_timezone = new Jam_Timezone(); $this->assertEquals(date('c', $this->date), $unchanged_timezone->date('c', $this->date)); $this->assertEquals(date('c', $this->date - 3600 * 2), $this->timezone->date('c', $this->date)); }
public function convert($value, $from, $to) { if (!$this->is_active()) { return $value; } return Jam_Timezone::shift($value, $this->{$from}(), $this->{$to}()); }