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

<?php
/**
* Format
*
* @copyright
* @link
* @package Majestic
* @subpackage Core
* @since 24.12.2008
* @version SVN: $Id$
* @filesource $URL$
*/
/**
* Отвечает за конвертацию данных между читаемым
* человеком и машиной форматами.
*
*/
class Format
{
/* Date & time */
static protected $time_format = 'H:i:s';
static protected $date_format = 'd.m.Y';
static protected $date_time_format = 'H:i d.m.Y';
static protected $timezone_offset = 0;
static protected $cache_time = array();
/* money */
static protected $decimal_point = ',';
static protected $currency_symbol = 'руб.';
static protected $frac_digits = 2;
static protected $cache_money = array();
static public function getCurrency()
{
return self::$currency_symbol;
}
static public function int2money($int = 0, $currency = false)
{
if(!isset(self::$cache_money[$int])){
self::$cache_money[$int] = number_format($int/100, self::$frac_digits, self::$decimal_point, '');
}
return self::$cache_money[$int] . (($currency) ? ' '.self::$currency_symbol : '');
}
static public function money2int($money)
{
if(!isset(self::$cache_money[$money])){
if(!strstr($money, self::$decimal_point) && !strstr($money, '.')){
self::$cache_money[$money] = (int)$money * 100;
}else{
self::$cache_money[$money] = (int)str_replace(array('.', self::$decimal_point), '', $money);
}
}
return self::$cache_money[$money];
}
/**
* Возвращает время в часах из секунд.
*
* @param int $timestamp
* @return string
*/
static public function int2time($timestamp = 0)
{
if(!isset(self::$cache_time[$timestamp])){
self::$cache_time[$timestamp] = date(self::$time_format, $timestamp-self::$timezone_offset);
}
return self::$cache_time[$timestamp];
}
/**
* Возвращает дату и время из таймстампа.
*
* @param int $timestamp
* @param bool $hours
* @return string
*/
static public function int2date($timestamp = 0, $hours = true)
{
if(!isset(self::$cache_time[$timestamp])){
self::$cache_time[$timestamp] = date(($hours) ? self::$date_time_format : self::$date_format , $timestamp);
}
return self::$cache_time[$timestamp];
}
/**
* Установка смещения для getTime
*
* @param int $offset
*/
static public function setTimezoneOffset($offset)
{
self::$timezone_offset = $offset;
}
/**
* Преобразует время в секунды.
*
* @param string $time
* @return int
*/
static public function time2int($time)
{
if(!isset(self::$cache_time[$time])){
$elements = explode(':', $time);
if(count($elements) == 3){
list($h, $m, $s) = $elements;
self::$cache_time[$time] = ($h * 60 * 60) + ($m * 60) + $s;
} else {
self::$cache_time[$time] = (int)$time;
}
}
return self::$cache_time[$time];
}
/**
* Преобразует дату в таймстамп.
*
* @param mixed $time
* @return TimeFormat
*/
static public function date2int($time)
{
if(!isset(self::$cache_time[$time])){
self::$cache_time[$time] = strtotime($time);
}
return self::$cache_time[$time];
}
static public function int2phone($intphone)
{
$intphone = (string) $intphone;
if (strlen($intphone) == 10) {
return '('.substr($intphone, 0, 3).') '.substr($intphone, 3, 3).'-'.substr($intphone, 6, 2).'-'.substr($intphone, 8, 2);
} elseif (strlen($intphone) == 7) {
return substr($intphone, 0, 3).'-'.substr($intphone, 3, 2).'-'.substr($intphone, 5, 2);
}
return '';
}
static public function phone2int($phone)
{
$phone = str_replace(array(' ','-','(',')'), '', $phone);
$phone_length = strlen($phone);
if (is_numeric($phone) && ($phone_length == 7 || $phone_length == 10)) { //бывают семизначные прямые номера
if ($phone_length == 7) {
$phone = '495'.$phone;
}
return $phone;
}
return '';
}
}
Format::setTimezoneOffset(date('Z'));
?>