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.

231 lines
6.9 KiB

  1. <?php
  2. /**
  3. * Format
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  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 $today_format = 'd.m.Y'; //disabled by default
  24. static protected $date_time_format = 'H:i d.m.Y';
  25. static protected $today_time_format = 'H:i d.m.Y'; //disabled by default
  26. static protected $timezone_offset = 0;
  27. /* money */
  28. static protected $decimal_point = ',';
  29. static protected $currency_symbol = 'руб.';
  30. static protected $frac_digits = 2;
  31. static public function getCurrency()
  32. {
  33. return self::$currency_symbol;
  34. }
  35. public static function setCurrencySymbol($symbol)
  36. {
  37. self::$currency_symbol = $symbol;
  38. }
  39. /**
  40. * Форматируем int в денежный формат
  41. *
  42. * @param mixed $int
  43. * @param bool $currency - показывать валюту
  44. * @param bool $show_decimals - показывать или нет дробную часть
  45. * @return string
  46. */
  47. static public function int2money($int = 0, $currency = false, $show_decimals = true)
  48. {
  49. $money = number_format($int/100, ($show_decimals) ? self::$frac_digits : 0, self::$decimal_point, ' ');
  50. if ($currency) {
  51. return str_replace(' ', html_entity_decode('&nbsp;', ENT_COMPAT, 'UTF-8'), $money) . '&nbsp;' . self::$currency_symbol;
  52. } else {
  53. return $money;
  54. }
  55. }
  56. static public function money2int($money)
  57. {
  58. $money = str_replace(' ', '', $money);
  59. if(!strstr($money, self::$decimal_point) && !strstr($money, '.')){
  60. $int = (int)$money * 100;
  61. }else{
  62. $int = (int)str_replace(array('.', self::$decimal_point), '', $money);
  63. }
  64. return $int;
  65. }
  66. /**
  67. * Возвращает время в часах из секунд.
  68. *
  69. * @param int $timestamp
  70. * @return string
  71. */
  72. static public function int2time($timestamp = 0)
  73. {
  74. $hours = floor($timestamp / 3600);
  75. $minutes = floor(($timestamp / 60) - ($hours * 60));
  76. $seconds = $timestamp - ($hours * 3600) - ($minutes * 60);
  77. return ($hours < 10 ? ('0' . $hours) : $hours)
  78. . ':' . ($minutes < 10 ? ('0' . $minutes) : $minutes)
  79. . ':' . ($seconds < 10 ? ('0' . $seconds) : $seconds);
  80. }
  81. /**
  82. * Возвращает дату и время из таймстампа.
  83. *
  84. * @param int $timestamp
  85. * @param bool $hours
  86. * @return string
  87. */
  88. static public function int2date($timestamp = 0, $hours = true)
  89. {
  90. if (date('Ymd') == date('Ymd', $timestamp)) {
  91. return date(($hours) ? self::$today_time_format : self::$today_format , $timestamp);
  92. }
  93. return date(($hours) ? self::$date_time_format : self::$date_format , $timestamp);
  94. }
  95. static public function int2rusDate($timestamp = 0, $hours = false)
  96. {
  97. $month = array('января', 'февраля', 'марта',
  98. 'апреля', 'мая', 'июня',
  99. 'июля', 'августа', 'сентября',
  100. 'октября', 'ноября', 'декабря');
  101. $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);
  102. return $date;
  103. }
  104. /**
  105. * Установка смещения для getTime
  106. *
  107. * @param int $offset
  108. */
  109. static public function setTimezoneOffset($offset)
  110. {
  111. self::$timezone_offset = $offset;
  112. }
  113. /**
  114. * Установка форматов даты
  115. *
  116. */
  117. static public function setDateFormat($date_time_format, $date_format = false)
  118. {
  119. self::$date_time_format = $date_time_format;
  120. if ($date_format) {
  121. self::$date_format = $date_format;
  122. }
  123. }
  124. /**
  125. * Установка форматов даты для текущего дня
  126. *
  127. */
  128. static public function setTodayFormat($today_time_format, $today_format = false)
  129. {
  130. self::$today_time_format = $today_time_format;
  131. if ($today_format) {
  132. self::$today_format = $today_format;
  133. }
  134. }
  135. /**
  136. * Преобразует время в секунды.
  137. *
  138. * @param string $time
  139. * @return int
  140. */
  141. static public function time2int($time)
  142. {
  143. $elements = explode(':', $time);
  144. if(count($elements) == 3){
  145. list($h, $m, $s) = $elements;
  146. $int = ($h * 60 * 60) + ($m * 60) + $s;
  147. } else {
  148. $int = (int)$time;
  149. }
  150. return $int;
  151. }
  152. /**
  153. * Преобразует дату в таймстамп.
  154. *
  155. * @param mixed $time
  156. * @return int|bool
  157. */
  158. static public function date2int($time)
  159. {
  160. return strtotime($time);
  161. }
  162. static public function int2phone($intphone)
  163. {
  164. $intphone = (string) $intphone;
  165. if (strlen($intphone) == 10) {
  166. return '(' . substr($intphone, 0, 3) . ') ' . substr($intphone, 3, 3) . '-' . substr($intphone, 6, 2) . '-' . substr($intphone, 8, 2);
  167. } elseif (strlen($intphone) == 7) {
  168. return substr($intphone, 0, 3) . '-' . substr($intphone, 3, 2) . '-' . substr($intphone, 5, 2);
  169. }
  170. return '';
  171. }
  172. static public function phone2int($phone)
  173. {
  174. $phone = str_replace(array(' ', '-', '(', ')'), '', $phone);
  175. $phone_length = strlen($phone);
  176. if (is_numeric($phone) && ($phone_length == 7 || $phone_length == 10)) { //бывают семизначные прямые номера
  177. if ($phone_length == 7) {
  178. $phone = '495' . $phone;
  179. }
  180. return $phone;
  181. }
  182. return '';
  183. }
  184. /**
  185. * Байты в мегабайты
  186. * @param int $bytes
  187. * @return string
  188. */
  189. public static function bytes2MB($bytes)
  190. {
  191. $mb = number_format((int) $bytes / 1024 / 1024, 2, '.', '');
  192. return preg_replace("/(\.?)0+$/", "", $mb) . 'МБ';
  193. }
  194. /**
  195. * Байты в килобайты
  196. * @param int $bytes
  197. * @return string
  198. */
  199. public static function bytes2KB($bytes)
  200. {
  201. $kb = number_format((int) $bytes / 1024, 2, '.', '');
  202. return preg_replace("/(\.?)0+$/", "", $kb) . 'КБ';
  203. }
  204. }
  205. /**
  206. * Оффсет с учетом летнего/зимнего времени
  207. */
  208. Format::setTimezoneOffset(date('Z') - ((date('I') ? 60*60 : 0 )));