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.

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