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.

127 lines
3.5 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 = 'd.m.Y H:i';
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 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])){
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];
}
}
Format::setTimezoneOffset(date('Z'));
?>