Example #1
0
 /**
  * Returns a relative, natural language representation of a timestamp
  *
  * @todo Wider range of values ... maybe future time as well?
  * @todo Support minimum resolution parameter.
  *
  * @param mixed $time          The time. Any format accepted by Horde_Date.
  * @param string $date_format  Format to display date if timestamp is
  *                             more then 1 day old.
  * @param string $time_format  Format to display time if timestamp is 1
  *                             day old.
  *
  * @return string  The relative time (i.e. 2 minutes ago)
  */
 public static function relativeDateTime($time, $date_format = '%x', $time_format = '%X')
 {
     $date = new Horde_Date($time);
     $delta = time() - $date->timestamp();
     if ($delta < 60) {
         return sprintf(Horde_Date_Translation::ngettext("%d second ago", "%d seconds ago", $delta), $delta);
     }
     $delta = round($delta / 60);
     if ($delta < 60) {
         return sprintf(Horde_Date_Translation::ngettext("%d minute ago", "%d minutes ago", $delta), $delta);
     }
     $delta = round($delta / 60);
     if ($delta < 24) {
         return sprintf(Horde_Date_Translation::ngettext("%d hour ago", "%d hours ago", $delta), $delta);
     }
     if ($delta > 24 && $delta < 48) {
         $date = new Horde_Date($time);
         return sprintf(Horde_Date_Translation::t("yesterday at %s"), $date->strftime($time_format));
     }
     $delta = round($delta / 24);
     if ($delta < 7) {
         return sprintf(Horde_Date_Translation::t("%d days ago"), $delta);
     }
     if (round($delta / 7) < 5) {
         $delta = round($delta / 7);
         return sprintf(Horde_Date_Translation::ngettext("%d week ago", "%d weeks ago", $delta), $delta);
     }
     // Default to the user specified date format.
     return $date->strftime($date_format);
 }
Example #2
0
 /**
  * Builds a new date object. If $date contains date parts, use them to
  * initialize the object.
  *
  * Recognized formats:
  * - arrays with keys 'year', 'month', 'mday', 'day'
  *   'hour', 'min', 'minute', 'sec'
  * - objects with properties 'year', 'month', 'mday', 'hour', 'min', 'sec'
  * - yyyy-mm-dd hh:mm:ss
  * - yyyymmddhhmmss
  * - yyyymmddThhmmssZ
  * - yyyymmdd (might conflict with unix timestamps between 31 Oct 1966 and
  *   03 Mar 1973)
  * - unix timestamps
  * - anything parsed by strtotime()/DateTime.
  *
  * @throws Horde_Date_Exception
  */
 public function __construct($date = null, $timezone = null)
 {
     if (!self::$_supportedSpecs) {
         self::$_supportedSpecs = self::$_defaultSpecs;
         if (function_exists('nl_langinfo')) {
             self::$_supportedSpecs .= 'bBpxX';
         }
     }
     if (func_num_args() > 2) {
         // Handle args in order: year month day hour min sec tz
         $this->_initializeFromArgs(func_get_args());
         return;
     }
     $this->_initializeTimezone($timezone);
     if (is_null($date)) {
         return;
     }
     if (is_string($date)) {
         $date = trim($date, '"');
     }
     if (is_object($date)) {
         $this->_initializeFromObject($date);
     } elseif (is_array($date)) {
         $this->_initializeFromArray($date);
     } elseif (preg_match('/^(\\d{4})-?(\\d{2})-?(\\d{2})T? ?(\\d{2}):?(\\d{2}):?(\\d{2})(?:\\.\\d+)?(Z?)$/', $date, $parts)) {
         $this->_year = (int) $parts[1];
         $this->_month = (int) $parts[2];
         $this->_mday = (int) $parts[3];
         $this->_hour = (int) $parts[4];
         $this->_min = (int) $parts[5];
         $this->_sec = (int) $parts[6];
         if ($parts[7]) {
             $this->_initializeTimezone('UTC');
         }
     } elseif (preg_match('/^(\\d{4})-?(\\d{2})-?(\\d{2})$/', $date, $parts) && $parts[2] > 0 && $parts[2] <= 12 && $parts[3] > 0 && $parts[3] <= 31) {
         $this->_year = (int) $parts[1];
         $this->_month = (int) $parts[2];
         $this->_mday = (int) $parts[3];
         $this->_hour = $this->_min = $this->_sec = 0;
     } elseif ((string) (int) $date == $date) {
         // Try as a timestamp.
         if ($this->_timezone) {
             $oldtimezone = date_default_timezone_get();
             date_default_timezone_set($this->_timezone);
         }
         $parts = @getdate($date);
         if ($parts) {
             $this->_year = $parts['year'];
             $this->_month = $parts['mon'];
             $this->_mday = $parts['mday'];
             $this->_hour = $parts['hours'];
             $this->_min = $parts['minutes'];
             $this->_sec = $parts['seconds'];
         }
         if ($this->_timezone) {
             date_default_timezone_set($oldtimezone);
         }
     } else {
         if (!empty($timezone)) {
             try {
                 $parsed = new DateTime($date, new DateTimeZone($timezone));
             } catch (Exception $e) {
                 throw new Horde_Date_Exception(sprintf(Horde_Date_Translation::t("Failed to parse time string (%s)"), $date));
             }
         } else {
             try {
                 $parsed = new DateTime($date);
             } catch (Exception $e) {
                 throw new Horde_Date_Exception(sprintf(Horde_Date_Translation::t("Failed to parse time string (%s)"), $date));
             }
             $parsed->setTimezone(new DateTimeZone(date_default_timezone_get()));
             $this->_initializeTimezone(date_default_timezone_get());
         }
         $this->_year = (int) $parsed->format('Y');
         $this->_month = (int) $parsed->format('m');
         $this->_mday = (int) $parsed->format('d');
         $this->_hour = (int) $parsed->format('H');
         $this->_min = (int) $parsed->format('i');
         $this->_sec = (int) $parsed->format('s');
     }
 }
 /**
  * Returns a description of this event's recurring type.
  *
  * @return string  Human readable recurring type.
  */
 public function getRecurName()
 {
     switch ($this->getRecurType()) {
         case self::RECUR_NONE:
             return Horde_Date_Translation::t("No recurrence");
         case self::RECUR_DAILY:
             return Horde_Date_Translation::t("Daily");
         case self::RECUR_WEEKLY:
             return Horde_Date_Translation::t("Weekly");
         case self::RECUR_MONTHLY_DATE:
         case self::RECUR_MONTHLY_WEEKDAY:
             return Horde_Date_Translation::t("Monthly");
         case self::RECUR_YEARLY_DATE:
         case self::RECUR_YEARLY_DAY:
         case self::RECUR_YEARLY_WEEKDAY:
             return Horde_Date_Translation::t("Yearly");
     }
 }