Files
majestic/classes/Format.class.php
pzinovkin 5e45bc877a int2time for more than 24hrs timestamps bugfix
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@71 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
2009-04-13 08:19:51 +00:00

171 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
/* money */
static protected $decimal_point = ',';
static protected $currency_symbol = 'руб.';
static protected $frac_digits = 2;
static public function getCurrency()
{
return self::$currency_symbol;
}
/**
* Форматируем int в денежный формат
*
* @param mixed $int
* @param bool $currency - показывать валюту
* @param bool $show_decimals - показывать или нет дробную часть
*/
static public function int2money($int = 0, $currency = false, $show_decimals = true)
{
$money = number_format($int/100, ($show_decimals) ? self::$frac_digits : 0, self::$decimal_point, ' ');
return $money . (($currency) ? ' '.self::$currency_symbol : '');
}
static public function money2int($money)
{
$money = str_replace(' ', '', $money);
if(!strstr($money, self::$decimal_point) && !strstr($money, '.')){
$int = (int)$money * 100;
}else{
$int = (int)str_replace(array('.', self::$decimal_point), '', $money);
}
return $int;
}
/**
* Возвращает время в часах из секунд.
*
* @param int $timestamp
* @return string
*/
static public function int2time($timestamp = 0)
{
$hours = floor($timestamp / 3600);
$minutes = floor(($timestamp / 60) - ($hours * 60));
$seconds = $timestamp - ($hours * 3600) - ($minutes * 60);
return ($hours < 10 ? ('0' . $hours) : $hours)
. ':' . ($minutes < 10 ? ('0' . $minutes) : $minutes)
. ':' . ($seconds < 10 ? ('0' . $seconds) : $seconds);
}
/**
* Возвращает дату и время из таймстампа.
*
* @param int $timestamp
* @param bool $hours
* @return string
*/
static public function int2date($timestamp = 0, $hours = true)
{
$date = date(($hours) ? self::$date_time_format : self::$date_format , $timestamp);
return $date;
}
static public function int2rusDate($timestamp = 0, $hours = false)
{
$month = array('января', 'февраля', 'марта',
'апреля', 'мая', 'июня',
'июля', 'августа', 'сентября',
'октября', 'ноября', 'декабря');
$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);
return $date;
}
/**
* Установка смещения для getTime
*
* @param int $offset
*/
static public function setTimezoneOffset($offset)
{
self::$timezone_offset = $offset;
}
/**
* Преобразует время в секунды.
*
* @param string $time
* @return int
*/
static public function time2int($time)
{
$elements = explode(':', $time);
if(count($elements) == 3){
list($h, $m, $s) = $elements;
$int = ($h * 60 * 60) + ($m * 60) + $s;
} else {
$int = (int)$time;
}
return $int;
}
/**
* Преобразует дату в таймстамп.
*
* @param mixed $time
* @return TimeFormat
*/
static public function date2int($time)
{
return strtotime($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') - ((date('I') ? 60*60 : 0 )));
?>