PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/I18n/View/Helper/DateFormat.php

https://bitbucket.org/Ppito/kawaiviewmodel2
PHP | 144 lines | 64 code | 17 blank | 63 comment | 6 complexity | b49aff3797cb7fd11644dbca1ea71a21 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_I18n
  9. */
  10. namespace Zend\I18n\View\Helper;
  11. use DateTime;
  12. use IntlDateFormatter;
  13. use Locale;
  14. use Zend\I18n\Exception;
  15. use Zend\View\Helper\AbstractHelper;
  16. /**
  17. * View helper for formatting dates.
  18. *
  19. * @category Zend
  20. * @package Zend_I18n
  21. * @subpackage View
  22. */
  23. class DateFormat extends AbstractHelper
  24. {
  25. /**
  26. * Locale to use instead of the default.
  27. *
  28. * @var string
  29. */
  30. protected $locale;
  31. /**
  32. * Timezone to use.
  33. *
  34. * @var string
  35. */
  36. protected $timezone;
  37. /**
  38. * Formatter instances.
  39. *
  40. * @var array
  41. */
  42. protected $formatters = array();
  43. /**
  44. * Set timezone to use instead of the default.
  45. *
  46. * @param string $timezone
  47. * @return DateFormat
  48. */
  49. public function setTimezone($timezone)
  50. {
  51. $this->timezone = (string) $timezone;
  52. foreach ($this->formatters as $formatter) {
  53. $formatter->setTimeZoneId($this->timezone);
  54. }
  55. return $this;
  56. }
  57. /**
  58. * Get the timezone to use.
  59. *
  60. * @return string|null
  61. */
  62. public function getTimezone()
  63. {
  64. if (!$this->timezone) {
  65. return date_default_timezone_get();
  66. }
  67. return $this->timezone;
  68. }
  69. /**
  70. * Set locale to use instead of the default.
  71. *
  72. * @param string $locale
  73. * @return DateFormat
  74. */
  75. public function setlocale($locale)
  76. {
  77. $this->locale = (string) $locale;
  78. return $this;
  79. }
  80. /**
  81. * Get the locale to use.
  82. *
  83. * @return string|null
  84. */
  85. public function getlocale()
  86. {
  87. if ($this->locale === null) {
  88. $this->locale = Locale::getDefault();
  89. }
  90. return $this->locale;
  91. }
  92. /**
  93. * Format a date.
  94. *
  95. * @param DateTime|integer|array $date
  96. * @param integer $dateType
  97. * @param integer $timeType
  98. * @param string $locale
  99. * @return string
  100. * @throws Exception\RuntimeException
  101. */
  102. public function __invoke(
  103. $date,
  104. $dateType = IntlDateFormatter::NONE,
  105. $timeType = IntlDateFormatter::NONE,
  106. $locale = null
  107. ) {
  108. if ($locale === null) {
  109. $locale = $this->getlocale();
  110. }
  111. $timezone = $this->getTimezone();
  112. $formatterId = md5($dateType . "\0" . $timeType . "\0" . $locale);
  113. if (!isset($this->formatters[$formatterId])) {
  114. $this->formatters[$formatterId] = new IntlDateFormatter(
  115. $locale,
  116. $dateType,
  117. $timeType,
  118. $timezone
  119. );
  120. }
  121. // DateTime support for IntlDateFormatter::format() was only added in 5.3.4
  122. if ($date instanceof DateTime && version_compare(PHP_VERSION, '5.3.4', '<')) {
  123. $date = $date->getTimestamp();
  124. }
  125. return $this->formatters[$formatterId]->format($date);
  126. }
  127. }