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.

170 lines
5.1 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. $hours = floor($timestamp / 3600);
  64. $minutes = floor(($timestamp / 60) - ($hours * 60));
  65. $seconds = $timestamp - ($hours * 3600) - ($minutes * 60);
  66. return ($hours < 10 ? ('0' . $hours) : $hours)
  67. . ':' . ($minutes < 10 ? ('0' . $minutes) : $minutes)
  68. . ':' . ($seconds < 10 ? ('0' . $seconds) : $seconds);
  69. }
  70. /**
  71. * Возвращает дату и время из таймстампа.
  72. *
  73. * @param int $timestamp
  74. * @param bool $hours
  75. * @return string
  76. */
  77. static public function int2date($timestamp = 0, $hours = true)
  78. {
  79. $date = date(($hours) ? self::$date_time_format : self::$date_format , $timestamp);
  80. return $date;
  81. }
  82. static public function int2rusDate($timestamp = 0, $hours = false)
  83. {
  84. $month = array('января', 'февраля', 'марта',
  85. 'апреля', 'мая', 'июня',
  86. 'июля', 'августа', 'сентября',
  87. 'октября', 'ноября', 'декабря');
  88. $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);
  89. return $date;
  90. }
  91. /**
  92. * Установка смещения для getTime
  93. *
  94. * @param int $offset
  95. */
  96. static public function setTimezoneOffset($offset)
  97. {
  98. self::$timezone_offset = $offset;
  99. }
  100. /**
  101. * Преобразует время в секунды.
  102. *
  103. * @param string $time
  104. * @return int
  105. */
  106. static public function time2int($time)
  107. {
  108. $elements = explode(':', $time);
  109. if(count($elements) == 3){
  110. list($h, $m, $s) = $elements;
  111. $int = ($h * 60 * 60) + ($m * 60) + $s;
  112. } else {
  113. $int = (int)$time;
  114. }
  115. return $int;
  116. }
  117. /**
  118. * Преобразует дату в таймстамп.
  119. *
  120. * @param mixed $time
  121. * @return TimeFormat
  122. */
  123. static public function date2int($time)
  124. {
  125. return strtotime($time);
  126. }
  127. static public function int2phone($intphone)
  128. {
  129. $intphone = (string) $intphone;
  130. if (strlen($intphone) == 10) {
  131. return '(' . substr($intphone, 0, 3) . ') ' . substr($intphone, 3, 3) . '-' . substr($intphone, 6, 2) . '-' . substr($intphone, 8, 2);
  132. } elseif (strlen($intphone) == 7) {
  133. return substr($intphone, 0, 3) . '-' . substr($intphone, 3, 2) . '-' . substr($intphone, 5, 2);
  134. }
  135. return '';
  136. }
  137. static public function phone2int($phone)
  138. {
  139. $phone = str_replace(array(' ', '-', '(', ')'), '', $phone);
  140. $phone_length = strlen($phone);
  141. if (is_numeric($phone) && ($phone_length == 7 || $phone_length == 10)) { //бывают семизначные прямые номера
  142. if ($phone_length == 7) {
  143. $phone = '495' . $phone;
  144. }
  145. return $phone;
  146. }
  147. return '';
  148. }
  149. }
  150. /**
  151. * Оффсет с учетом летнего/зимнего времени
  152. */
  153. Format::setTimezoneOffset(date('Z') - ((date('I') ? 60*60 : 0 )));
  154. ?>