Overview

Packages

  • Date

Classes

  • Horde_Date
  • Horde_Date_Exception
  • Horde_Date_Recurrence
  • Horde_Date_Repeater
  • Horde_Date_Repeater_Day
  • Horde_Date_Repeater_DayName
  • Horde_Date_Repeater_DayPortion
  • Horde_Date_Repeater_Fortnight
  • Horde_Date_Repeater_Hour
  • Horde_Date_Repeater_Minute
  • Horde_Date_Repeater_Month
  • Horde_Date_Repeater_MonthName
  • Horde_Date_Repeater_Season
  • Horde_Date_Repeater_SeasonName
  • Horde_Date_Repeater_Second
  • Horde_Date_Repeater_Time
  • Horde_Date_Repeater_Week
  • Horde_Date_Repeater_Weekend
  • Horde_Date_Repeater_Year
  • Horde_Date_Span
  • Horde_Date_Translation
  • Horde_Date_Utils

Exceptions

  • Horde_Date_Repeater_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @category Horde
  4:  * @package  Date
  5:  */
  6: 
  7: /**
  8:  * Horde Date wrapper/logic class, including some calculation
  9:  * functions.
 10:  *
 11:  * @category Horde
 12:  * @package  Date
 13:  */
 14: class Horde_Date_Utils
 15: {
 16:     /**
 17:      * Returns whether a year is a leap year.
 18:      *
 19:      * @param integer $year  The year.
 20:      *
 21:      * @return boolean  True if the year is a leap year.
 22:      */
 23:     public static function isLeapYear($year)
 24:     {
 25:         if (strlen($year) != 4 || preg_match('/\D/', $year)) {
 26:             return false;
 27:         }
 28: 
 29:         return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
 30:     }
 31: 
 32:     /**
 33:      * Returns the date of the year that corresponds to the first day of the
 34:      * given week.
 35:      *
 36:      * @param integer $week  The week of the year to find the first day of.
 37:      * @param integer $year  The year to calculate for.
 38:      *
 39:      * @return Horde_Date  The date of the first day of the given week.
 40:      */
 41:     public static function firstDayOfWeek($week, $year)
 42:     {
 43:         return new Horde_Date(sprintf('%04dW%02d', $year, $week));
 44:     }
 45: 
 46:     /**
 47:      * Returns the number of days in the specified month.
 48:      *
 49:      * @param integer $month  The month
 50:      * @param integer $year   The year.
 51:      *
 52:      * @return integer  The number of days in the month.
 53:      */
 54:     public static function daysInMonth($month, $year)
 55:     {
 56:         static $cache = array();
 57:         if (!isset($cache[$year][$month])) {
 58:             $date = new DateTime(sprintf('%04d-%02d-01', $year, $month));
 59:             $cache[$year][$month] = $date->format('t');
 60:         }
 61:         return $cache[$year][$month];
 62:     }
 63: 
 64:     /**
 65:      * Returns a relative, natural language representation of a timestamp
 66:      *
 67:      * @todo Wider range of values ... maybe future time as well?
 68:      * @todo Support minimum resolution parameter.
 69:      *
 70:      * @param mixed $time          The time. Any format accepted by Horde_Date.
 71:      * @param string $date_format  Format to display date if timestamp is
 72:      *                             more then 1 day old.
 73:      * @param string $time_format  Format to display time if timestamp is 1
 74:      *                             day old.
 75:      *
 76:      * @return string  The relative time (i.e. 2 minutes ago)
 77:      */
 78:     public static function relativeDateTime($time, $date_format = '%x',
 79:                                             $time_format = '%X')
 80:     {
 81:         $date = new Horde_Date($time);
 82: 
 83:         $delta = time() - $date->timestamp();
 84:         if ($delta < 60) {
 85:             return sprintf(Horde_Date_Translation::ngettext("%d second ago", "%d seconds ago", $delta), $delta);
 86:         }
 87: 
 88:         $delta = round($delta / 60);
 89:         if ($delta < 60) {
 90:             return sprintf(Horde_Date_Translation::ngettext("%d minute ago", "%d minutes ago", $delta), $delta);
 91:         }
 92: 
 93:         $delta = round($delta / 60);
 94:         if ($delta < 24) {
 95:             return sprintf(Horde_Date_Translation::ngettext("%d hour ago", "%d hours ago", $delta), $delta);
 96:         }
 97: 
 98:         if ($delta > 24 && $delta < 48) {
 99:             $date = new Horde_Date($time);
100:             return sprintf(Horde_Date_Translation::t("yesterday at %s"), $date->strftime($time_format));
101:         }
102: 
103:         $delta = round($delta / 24);
104:         if ($delta < 7) {
105:             return sprintf(Horde_Date_Translation::t("%d days ago"), $delta);
106:         }
107: 
108:         if (round($delta / 7) < 5) {
109:             $delta = round($delta / 7);
110:             return sprintf(Horde_Date_Translation::ngettext("%d week ago", "%d weeks ago", $delta), $delta);
111:         }
112: 
113:         // Default to the user specified date format.
114:         return $date->strftime($date_format);
115:     }
116: 
117:     /**
118:      * Tries to convert strftime() formatters to date() formatters.
119:      *
120:      * Unsupported formatters will be removed.
121:      *
122:      * @param string $format  A strftime() formatting string.
123:      *
124:      * @return string  A date() formatting string.
125:      */
126:     public static function strftime2date($format)
127:     {
128:         $replace = array(
129:             '/%a/'  => 'D',
130:             '/%A/'  => 'l',
131:             '/%d/'  => 'd',
132:             '/%e/'  => 'j',
133:             '/%j/'  => 'z',
134:             '/%u/'  => 'N',
135:             '/%w/'  => 'w',
136:             '/%U/'  => '',
137:             '/%V/'  => 'W',
138:             '/%W/'  => '',
139:             '/%b/'  => 'M',
140:             '/%B/'  => 'F',
141:             '/%h/'  => 'M',
142:             '/%m/'  => 'm',
143:             '/%C/'  => '',
144:             '/%g/'  => '',
145:             '/%G/'  => 'o',
146:             '/%y/'  => 'y',
147:             '/%Y/'  => 'Y',
148:             '/%H/'  => 'H',
149:             '/%I/'  => 'h',
150:             '/%i/'  => 'g',
151:             '/%M/'  => 'i',
152:             '/%p/'  => 'A',
153:             '/%P/'  => 'a',
154:             '/%r/'  => 'h:i:s A',
155:             '/%R/'  => 'H:i',
156:             '/%S/'  => 's',
157:             '/%T/'  => 'H:i:s',
158:             '/%X/e' => 'Horde_Date_Utils::strftime2date(Horde_Nls::getLangInfo(T_FMT))',
159:             '/%z/'  => 'O',
160:             '/%Z/'  => '',
161:             '/%c/'  => '',
162:             '/%D/'  => 'm/d/y',
163:             '/%F/'  => 'Y-m-d',
164:             '/%s/'  => 'U',
165:             '/%x/e' => 'Horde_Date_Utils::strftime2date(Horde_Nls::getLangInfo(D_FMT))',
166:             '/%n/'  => "\n",
167:             '/%t/'  => "\t",
168:             '/%%/'  => '%'
169:         );
170: 
171:         return preg_replace(array_keys($replace), array_values($replace), $format);
172:     }
173: 
174: }
175: 
API documentation generated by ApiGen