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.

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