PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/symphony/lib/core/class.datetimeobj.php

https://github.com/rainerborene/symphony-2
PHP | 230 lines | 77 code | 24 blank | 129 comment | 16 complexity | f3311b33b47c95ece062a6d670ec9f68 MD5 | raw file
  1. <?php
  2. /**
  3. * @package core
  4. */
  5. /**
  6. * The DateTimeObj provides static functions regarding dates in Symphony.
  7. * Symphony will set the default timezone of the system using it's configuration
  8. * values.
  9. */
  10. Class DateTimeObj {
  11. /**
  12. * Uses PHP's date_default_timezone_set function to set the system
  13. * timezone. If the timezone provided is invalid, a `E_USER_WARNING` will be
  14. * raised.
  15. *
  16. * @link http://php.net/manual/en/function.date-default-timezone-set.php
  17. * @link http://www.php.net/manual/en/timezones.php
  18. * @param string $timezone
  19. * A valid timezone identifier, such as UTC or Europe/Lisbon
  20. */
  21. public static function setDefaultTimezone($timezone){
  22. if(!@date_default_timezone_set($timezone)) trigger_error(__("Invalid timezone '{$timezone}'"), E_USER_WARNING);
  23. }
  24. /**
  25. * Validate a given date and time string
  26. *
  27. * @param string $string
  28. * A date and time string to validate
  29. * @return boolean
  30. * Returns true for valid dates, otherwise false
  31. */
  32. public static function validate($string) {
  33. $string = trim($string);
  34. // String is empty or not a valid date string
  35. if(empty($string) || !strtotime(Lang::standardizeDate($string))) {
  36. return false;
  37. }
  38. // String is a valid date
  39. else {
  40. return true;
  41. }
  42. }
  43. /**
  44. * Given a `$format`, and a `$timestamp`,
  45. * return the date in the format provided. This function is a basic
  46. * wrapper for PHP's DateTime object. If the `$timestamp` is omitted,
  47. * the current timestamp will be used. Optionally, you pass a
  48. * timezone identifier with this function to localise the output
  49. *
  50. * If you like to display a date in the backend, please make use
  51. * of `DateTimeObj::format()` which allows date and time localization
  52. *
  53. * @see class.datetimeobj.php#format()
  54. * @link http://www.php.net/manual/en/book.datetime.php
  55. * @param string $format
  56. * A valid PHP date format
  57. * @param integer $timestamp (optional)
  58. * A unix timestamp to format. 'now' or omitting this parameter will
  59. * result in the current time being used
  60. * @param string $timezone (optional)
  61. * The timezone associated with the timestamp
  62. * @return string
  63. * The formatted date
  64. */
  65. public static function get($format, $timestamp = 'now', $timezone = null) {
  66. return self::format($timestamp, $format, false, $timezone);
  67. }
  68. /**
  69. * Formats the given date and time `$string` based on the given `$format`.
  70. * Optionally the result will be localized and respect a timezone differing
  71. * from the system default. The default output is ISO 8601.
  72. *
  73. * @since Symphony 2.2.1
  74. * @param string $string (optional)
  75. * A string containing date and time, defaults to the current date and time
  76. * @param string $format (optional)
  77. * A valid PHP date format, defaults to ISO 8601
  78. * @param boolean $localize (optional)
  79. * Localizes the output, if true, defaults to true
  80. * @param string $timezone (optional)
  81. * The timezone associated with the timestamp
  82. * @return string
  83. * The formatted date
  84. */
  85. public static function format($string = 'now', $format = DateTime::ISO8601, $localize = true, $timezone = null) {
  86. // Current date and time
  87. if($string == 'now' || empty($string)) {
  88. $date = new DateTime();
  89. }
  90. // Timestamp
  91. elseif(is_numeric($string)) {
  92. $date = new Datetime(date(DateTime::ISO8601, $string));
  93. }
  94. // Attempt to parse the date provided against the Symphony configuration setting
  95. // in an effort to better support multilingual date formats. Should this fail
  96. // this block will fallback to using `strtotime`, which will parse the date assuming
  97. // it's in an English format
  98. else {
  99. // Standardize date
  100. // Convert date string to English
  101. $string = Lang::standardizeDate($string);
  102. // PHP 5.3: Apply Symphony date format using `createFromFormat`
  103. if(method_exists('DateTime', 'createFromFormat')) {
  104. $date = DateTime::createFromFormat(__SYM_DATETIME_FORMAT__, $string);
  105. if($date === false) {
  106. $date = DateTime::createFromFormat(__SYM_DATE_FORMAT__, $string);
  107. }
  108. }
  109. // PHP 5.2: Fallback to `strptime`
  110. else {
  111. $date = strptime($string, DateTimeObj::dateFormatToStrftime(__SYM_DATETIME_FORMAT__));
  112. if($date === false) {
  113. $date = DateTimeObj::dateFormatToStrftime(__SYM_DATE_FORMAT__, $string);
  114. }
  115. if(is_array($date)) {
  116. $date = date(DateTime::ISO8601, mktime(
  117. // Time
  118. $date['tm_hour'], $date['tm_min'], $date['tm_sec'],
  119. // Date (Months since Jan / Years since 1900)
  120. $date['tm_mon'] + 1, $date['tm_mday'], 1900 + $date['tm_year']
  121. ));
  122. $date = new DateTime($date);
  123. }
  124. }
  125. // Handle non-standard dates (ie. relative dates, tomorrow etc.)
  126. if($date === false) {
  127. $date = new DateTime($string);
  128. }
  129. }
  130. // Timezone
  131. if($timezone !== null) {
  132. $date->setTimezone(new DateTimeZone($timezone));
  133. }
  134. // Format date
  135. $date = $date->format($format);
  136. // Localize date
  137. // Convert date string from English back to the activated Language
  138. if($localize === true) {
  139. $date = Lang::localizeDate($date);
  140. }
  141. // Return custom formatted date, use ISO 8601 date by default
  142. return $date;
  143. }
  144. /**
  145. * Convert a date format to a `strftime` format
  146. * Timezone conversion is done for unix. Windows users must exchange %z and %Z.
  147. *
  148. * Unsupported `date` formats : S, n, t, L, B, G, u, e, I, P, Z, c, r
  149. * Unsupported `strftime` formats : %U, %W, %C, %g, %r, %R, %T, %X, %c, %D, %F, %x
  150. *
  151. * @since Symphony 2.2.1
  152. * @link http://www.php.net/manual/en/function.strftime.php#96424
  153. * @param string $dateFormat a date format
  154. * @return string
  155. */
  156. public static function dateFormatToStrftime($dateFormat) {
  157. $caracs = array(
  158. // Day - no strf eq : S
  159. 'd' => '%d', 'D' => '%a', 'j' => '%e', 'l' => '%A', 'N' => '%u', 'w' => '%w', 'z' => '%j',
  160. // Week - no date eq : %U, %W
  161. 'W' => '%V',
  162. // Month - no strf eq : n, t
  163. 'F' => '%B', 'm' => '%m', 'M' => '%b',
  164. // Year - no strf eq : L; no date eq : %C, %g
  165. 'o' => '%G', 'Y' => '%Y', 'y' => '%y',
  166. // Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X
  167. 'a' => '%P', 'A' => '%p', 'g' => '%l', 'h' => '%I', 'H' => '%H', 'i' => '%M', 's' => '%S',
  168. // Timezone - no strf eq : e, I, P, Z
  169. 'O' => '%z', 'T' => '%Z',
  170. // Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x
  171. 'U' => '%s'
  172. );
  173. return strtr((string)$dateFormat, $caracs);
  174. }
  175. /**
  176. * A wrapper for get, this function will force the GMT timezone.
  177. *
  178. * @param string $format
  179. * A valid PHP date format
  180. * @param integer $timestamp (optional)
  181. * A unix timestamp to format. Omitting this parameter will
  182. * result in the current time being used
  183. * @return string
  184. * The formatted date in GMT
  185. */
  186. public static function getGMT($format, $timestamp = 'now'){
  187. return self::format($timestamp, $format, false, 'GMT');
  188. }
  189. /**
  190. * A wrapper for get, this function will return a HTML string representing
  191. * an `<abbr>` element which contained the formatted date of now, and an
  192. * RFC 2822 formatted date (Thu, 21 Dec 2000 16:01:07 +0200) as the title
  193. * attribute. Symphony uses this in it's status messages so that it can
  194. * dynamically update how long ago the action took place using Javascript.
  195. *
  196. * @param string $format
  197. * A valid PHP date format
  198. * @return string
  199. * A HTML string of an `<abbr>` element with a class of 'timeago' and the current
  200. * date (RFC 2822) as the title element. The value is the current time as
  201. * specified by the `$format`.
  202. */
  203. public static function getTimeAgo($format){
  204. return '<abbr class="timeago" title="' . self::get(DateTime::RFC2822) . '">' . self::get($format) . '</abbr>';
  205. }
  206. }