You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.5 KiB

  1. <?php
  2. /**
  3. * Format
  4. *
  5. * @copyright
  6. * @link
  7. * @package Majestic
  8. * @subpackage Core
  9. * @since 24.12.2008
  10. * @version SVN: $Id$
  11. * @filesource $URL$
  12. */
  13. /**
  14. * Отвечает за конвертацию данных между читаемым
  15. * человеком и машиной форматами.
  16. *
  17. */
  18. class Format
  19. {
  20. /* Date & time */
  21. static protected $time_format = 'H:i:s';
  22. static protected $date_format = 'd.m.Y';
  23. static protected $date_time_format = 'd.m.Y H:i';
  24. static protected $timezone_offset = 0;
  25. static protected $cache_time = array();
  26. /* money */
  27. static protected $decimal_point = ',';
  28. static protected $currency_symbol = 'руб.';
  29. static protected $frac_digits = 2;
  30. static protected $cache_money = array();
  31. static public function getCurrency()
  32. {
  33. return self::$currency_symbol;
  34. }
  35. static public function int2money($int = 0, $currency = false)
  36. {
  37. if(!isset(self::$cache_money[$int])){
  38. self::$cache_money[$int] = number_format($int/100, self::$frac_digits, self::$decimal_point, '');
  39. }
  40. return self::$cache_money[$int] . (($currency) ? ' '.self::$currency_symbol : '');
  41. }
  42. static public function money2int($money)
  43. {
  44. if(!isset(self::$cache_money[$money])){
  45. self::$cache_money[$money] = (int)str_replace(array('.', self::$decimal_point), '', $money);
  46. }
  47. return self::$cache_money[$money];
  48. }
  49. /**
  50. * Возвращает время в часах из секунд.
  51. *
  52. * @param int $timestamp
  53. * @return string
  54. */
  55. static public function int2time($timestamp = 0)
  56. {
  57. if(!isset(self::$cache_time[$timestamp])){
  58. self::$cache_time[$timestamp] = date(self::$time_format, $timestamp-self::$timezone_offset);
  59. }
  60. return self::$cache_time[$timestamp];
  61. }
  62. /**
  63. * Возвращает дату и время из таймстампа.
  64. *
  65. * @param int $timestamp
  66. * @param bool $hours
  67. * @return string
  68. */
  69. static public function int2date($timestamp = 0, $hours = true)
  70. {
  71. if(!isset(self::$cache_time[$timestamp])){
  72. self::$cache_time[$timestamp] = date(($hours) ? self::$date_time_format : self::$date_format , $timestamp);
  73. }
  74. return self::$cache_time[$timestamp];
  75. }
  76. /**
  77. * Установка смещения для getTime
  78. *
  79. * @param int $offset
  80. */
  81. static public function setTimezoneOffset($offset)
  82. {
  83. self::$timezone_offset = $offset;
  84. }
  85. /**
  86. * Преобразует время в секунды.
  87. *
  88. * @param string $time
  89. * @return int
  90. */
  91. static public function time2int($time)
  92. {
  93. if(!isset(self::$cache_time[$time])){
  94. $elements = explode(':', $time);
  95. if(count($elements) == 3){
  96. list($h, $m, $s) = $elements;
  97. self::$cache_time[$time] = ($h * 60 * 60) + ($m * 60) + $s;
  98. } else {
  99. self::$cache_time[$time] = (int)$time;
  100. }
  101. }
  102. return self::$cache_time[$time];
  103. }
  104. /**
  105. * Преобразует дату в таймстамп.
  106. *
  107. * @param mixed $time
  108. * @return TimeFormat
  109. */
  110. static public function date2int($time)
  111. {
  112. if(!isset(self::$cache_time[$time])){
  113. self::$cache_time[$time] = strtotime($time);
  114. }
  115. return self::$cache_time[$time];
  116. }
  117. }
  118. Format::setTimezoneOffset(date('Z'));
  119. ?>