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.

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