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.

179 lines
5.6 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 = 'H:i d.m.Y';
  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. /**
  36. * Форматируем int в денежный формат
  37. *
  38. * @param mixed $int
  39. * @param bool $currency - показывать валюту
  40. * @param bool $show_decimals - показывать или нет дробную часть
  41. */
  42. static public function int2money($int = 0, $currency = false, $show_decimals = true)
  43. {
  44. $key = $int . '_' . (int)$currency . '_' . (int)$show_decimals;
  45. if(!isset(self::$cache_money[$key])){
  46. self::$cache_money[$key] = number_format($int/100, ($show_decimals) ? self::$frac_digits : 0, self::$decimal_point, ' ');
  47. }
  48. return self::$cache_money[$key] . (($currency) ? ' '.self::$currency_symbol : '');
  49. }
  50. static public function money2int($money)
  51. {
  52. $money = str_replace(' ', '', $money);
  53. if(!isset(self::$cache_money[$money])){
  54. if(!strstr($money, self::$decimal_point) && !strstr($money, '.')){
  55. self::$cache_money[$money] = (int)$money * 100;
  56. }else{
  57. self::$cache_money[$money] = (int)str_replace(array('.', self::$decimal_point), '', $money);
  58. }
  59. }
  60. return self::$cache_money[$money];
  61. }
  62. /**
  63. * Возвращает время в часах из секунд.
  64. *
  65. * @param int $timestamp
  66. * @return string
  67. */
  68. static public function int2time($timestamp = 0)
  69. {
  70. if(!isset(self::$cache_time[$timestamp])){
  71. self::$cache_time[$timestamp] = date(self::$time_format, $timestamp-self::$timezone_offset);
  72. }
  73. return self::$cache_time[$timestamp];
  74. }
  75. /**
  76. * Возвращает дату и время из таймстампа.
  77. *
  78. * @param int $timestamp
  79. * @param bool $hours
  80. * @return string
  81. */
  82. static public function int2date($timestamp = 0, $hours = true)
  83. {
  84. if(!isset(self::$cache_time[$timestamp])){
  85. self::$cache_time[$timestamp] = date(($hours) ? self::$date_time_format : self::$date_format , $timestamp);
  86. }
  87. return self::$cache_time[$timestamp];
  88. }
  89. static public function int2rusDate($timestamp = 0, $hours = false)
  90. {
  91. $month = array("января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря");
  92. if(!isset(self::$cache_time[$timestamp])){
  93. self::$cache_time[$timestamp] = ($hours) ? date("H:i d", $timestamp) . " " . $month[date("m", $timestamp) - 1] . " " .date("Y", $timestamp) : date("d", $timestamp) . " " . $month[date("m", $timestamp) - 1] . " " .date("Y", $timestamp);
  94. }
  95. return self::$cache_time[$timestamp];
  96. }
  97. /**
  98. * Установка смещения для getTime
  99. *
  100. * @param int $offset
  101. */
  102. static public function setTimezoneOffset($offset)
  103. {
  104. self::$timezone_offset = $offset;
  105. }
  106. /**
  107. * Преобразует время в секунды.
  108. *
  109. * @param string $time
  110. * @return int
  111. */
  112. static public function time2int($time)
  113. {
  114. if(!isset(self::$cache_time[$time])){
  115. $elements = explode(':', $time);
  116. if(count($elements) == 3){
  117. list($h, $m, $s) = $elements;
  118. self::$cache_time[$time] = ($h * 60 * 60) + ($m * 60) + $s;
  119. } else {
  120. self::$cache_time[$time] = (int)$time;
  121. }
  122. }
  123. return self::$cache_time[$time];
  124. }
  125. /**
  126. * Преобразует дату в таймстамп.
  127. *
  128. * @param mixed $time
  129. * @return TimeFormat
  130. */
  131. static public function date2int($time)
  132. {
  133. if(!isset(self::$cache_time[$time])){
  134. self::$cache_time[$time] = strtotime($time);
  135. }
  136. return self::$cache_time[$time];
  137. }
  138. static public function int2phone($intphone)
  139. {
  140. $intphone = (string) $intphone;
  141. if (strlen($intphone) == 10) {
  142. return '('.substr($intphone, 0, 3).') '.substr($intphone, 3, 3).'-'.substr($intphone, 6, 2).'-'.substr($intphone, 8, 2);
  143. } elseif (strlen($intphone) == 7) {
  144. return substr($intphone, 0, 3).'-'.substr($intphone, 3, 2).'-'.substr($intphone, 5, 2);
  145. }
  146. return '';
  147. }
  148. static public function phone2int($phone)
  149. {
  150. $phone = str_replace(array(' ','-','(',')'), '', $phone);
  151. $phone_length = strlen($phone);
  152. if (is_numeric($phone) && ($phone_length == 7 || $phone_length == 10)) { //бывают семизначные прямые номера
  153. if ($phone_length == 7) {
  154. $phone = '495'.$phone;
  155. }
  156. return $phone;
  157. }
  158. return '';
  159. }
  160. }
  161. Format::setTimezoneOffset(date('Z'));
  162. ?>