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.

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