DateFormatterInterface.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Drupal\Core\Datetime;
  3. /**
  4. * Provides an interface defining a date formatter.
  5. */
  6. interface DateFormatterInterface {
  7. /**
  8. * Formats a date, using a date type or a custom date format string.
  9. *
  10. * @param int $timestamp
  11. * A UNIX timestamp to format.
  12. * @param string $type
  13. * (optional) The format to use, one of:
  14. * - One of the built-in formats: 'short', 'medium',
  15. * 'long', 'html_datetime', 'html_date', 'html_time',
  16. * 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
  17. * - The name of a date type defined by a date format config entity.
  18. * - The machine name of an administrator-defined date format.
  19. * - 'custom', to use $format.
  20. * Defaults to 'medium'.
  21. * @param string $format
  22. * (optional) If $type is 'custom', a PHP date format string suitable for
  23. * input to date(). Use a backslash to escape ordinary text, so it does not
  24. * get interpreted as date format characters.
  25. * @param string|null $timezone
  26. * (optional) Time zone identifier, as described at
  27. * http://php.net/manual/timezones.php Defaults to the time zone used to
  28. * display the page.
  29. * @param string|null $langcode
  30. * (optional) Language code to translate to. NULL (default) means to use
  31. * the user interface language for the page.
  32. *
  33. * @return string
  34. * A translated date string in the requested format. Since the format may
  35. * contain user input, this value should be escaped when output.
  36. */
  37. public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL);
  38. /**
  39. * Formats a time interval with the requested granularity.
  40. *
  41. * Note that for intervals over 30 days, the output is approximate: a "month"
  42. * is always exactly 30 days, and a "year" is always 365 days. It is not
  43. * possible to make a more exact representation, given that there is only one
  44. * input in seconds. If you are formatting an interval between two specific
  45. * timestamps, use \Drupal\Core\Datetime\DateFormatter::formatDiff() instead.
  46. *
  47. * @param int $interval
  48. * The length of the interval in seconds.
  49. * @param int $granularity
  50. * (optional) How many different units to display in the string (2 by
  51. * default).
  52. * @param string|null $langcode
  53. * (optional) langcode: The language code for the language used to format
  54. * the date. Defaults to NULL, which results in the user interface language
  55. * for the page being used.
  56. *
  57. * @return string
  58. * A translated string representation of the interval.
  59. *
  60. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatDiff()
  61. */
  62. public function formatInterval($interval, $granularity = 2, $langcode = NULL);
  63. /**
  64. * Provides values for all date formatting characters for a given timestamp.
  65. *
  66. * @param string|null $langcode
  67. * (optional) Language code of the date format, if different from the site
  68. * default language.
  69. * @param int|null $timestamp
  70. * (optional) The Unix timestamp to format, defaults to current time.
  71. * @param string|null $timezone
  72. * (optional) The timezone to use, if different from the site's default
  73. * timezone.
  74. *
  75. * @return array
  76. * An array of formatted date values, indexed by the date format character.
  77. *
  78. * @see date()
  79. */
  80. public function getSampleDateFormats($langcode = NULL, $timestamp = NULL, $timezone = NULL);
  81. /**
  82. * Formats the time difference from the current request time to a timestamp.
  83. *
  84. * @param $timestamp
  85. * A UNIX timestamp to compare against the current request time.
  86. * @param array $options
  87. * (optional) An associative array with additional options. The following
  88. * keys can be used:
  89. * - granularity: An integer value that signals how many different units to
  90. * display in the string. Defaults to 2.
  91. * - langcode: The language code for the language used to format the date.
  92. * Defaults to NULL, which results in the user interface language for the
  93. * page being used.
  94. * - strict: A Boolean value indicating whether or not the timestamp can be
  95. * before the current request time. If TRUE (default) and $timestamp is
  96. * before the current request time, the result string will be "0 seconds".
  97. * If FALSE and $timestamp is before the current request time, the result
  98. * string will be the formatted time difference.
  99. * - return_as_object: A Boolean value whether to return a FormattedDateDiff
  100. * object.
  101. *
  102. * @return string|\Drupal\Core\Datetime\FormattedDateDiff
  103. * A translated string representation of the difference between the given
  104. * timestamp and the current request time. This interval is always positive.
  105. *
  106. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatDiff()
  107. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatTimeDiffSince()
  108. */
  109. public function formatTimeDiffUntil($timestamp, $options = []);
  110. /**
  111. * Formats the time difference from a timestamp to the current request time.
  112. *
  113. * @param $timestamp
  114. * A UNIX timestamp to compare against the current request time.
  115. * @param array $options
  116. * (optional) An associative array with additional options. The following
  117. * keys can be used:
  118. * - granularity: An integer value that signals how many different units to
  119. * display in the string. Defaults to 2.
  120. * - langcode: The language code for the language used to format the date.
  121. * Defaults to NULL, which results in the user interface language for the
  122. * page being used.
  123. * - strict: A Boolean value indicating whether or not the timestamp can be
  124. * after the current request time. If TRUE (default) and $timestamp is
  125. * after the current request time, the result string will be "0 seconds".
  126. * If FALSE and $timestamp is after the current request time, the result
  127. * string will be the formatted time difference.
  128. * - return_as_object: A Boolean value whether to return a FormattedDateDiff
  129. * object.
  130. *
  131. * @return string|\Drupal\Core\Datetime\FormattedDateDiff
  132. * A translated string representation of the difference between the given
  133. * timestamp and the current request time. This interval is always positive.
  134. *
  135. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatDiff()
  136. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatTimeDiffUntil()
  137. */
  138. public function formatTimeDiffSince($timestamp, $options = []);
  139. /**
  140. * Formats a time interval between two timestamps.
  141. *
  142. * @param int $from
  143. * A UNIX timestamp, defining the from date and time.
  144. * @param int $to
  145. * A UNIX timestamp, defining the to date and time.
  146. * @param array $options
  147. * (optional) An associative array with additional options. The following
  148. * keys can be used:
  149. * - granularity: An integer value that signals how many different units to
  150. * display in the string. Defaults to 2.
  151. * - langcode: The language code for the language used to format the date.
  152. * Defaults to NULL, which results in the user interface language for the
  153. * page being used.
  154. * - strict: A Boolean value indicating whether or not the $from timestamp
  155. * can be after the $to timestamp. If TRUE (default) and $from is after
  156. * $to, the result string will be "0 seconds". If FALSE and $from is
  157. * after $to, the result string will be the formatted time difference.
  158. * - return_as_object: A Boolean value whether to return a FormattedDateDiff
  159. * object.
  160. *
  161. * @return string|\Drupal\Core\Datetime\FormattedDateDiff
  162. * A translated string representation of the interval. This interval is
  163. * always positive.
  164. *
  165. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatInterval()
  166. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatTimeDiffSince()
  167. * @see \Drupal\Core\Datetime\DateFormatterInterface::formatTimeDiffUntil()
  168. */
  169. public function formatDiff($from, $to, $options = []);
  170. }