mira
Новичок
[umala] Date and Time Calculation
При передаче некоторых отрицательных параметров в PHP-функции времени - PHP 4.0 вместе с Апачи вылетает в трубу под Microsoft Windows.
Поэтому и был написан следующий класс-врапер.
Каковы будут коментарии и замечания?
При передаче некоторых отрицательных параметров в PHP-функции времени - PHP 4.0 вместе с Апачи вылетает в трубу под Microsoft Windows.
Поэтому и был написан следующий класс-врапер.
Каковы будут коментарии и замечания?
Код:
<?php
//**************************************************************************
//
// Required: PHP Version 4.0.0
//
//**************************************************************************
//
// Date-Time calculation
//
//**************************************************************************
/**
* Date-Time calculation class (in range 1970.01.01 00:00:00 - 2038.01.19 03:14:07).
*
* @author Mira
* @package Umala
*/
class TTime
{
/**
* Returns associative array containing information about a time-zone and Daylight Saving Time.
*
* $result['timezone'] - time-zone (in seconds).
* $result['dstime'] - Daylight Saving Time (in seconds).
*
* @static static
* @access public
* @return array
*/
function temezone()
{
static $params = null;
if (!$params)
{
$params = gettimeofday();
$params = array('timezone' => (-$params['minuteswest'] * 60), 'dstime' => $params['dsttime'] * 3600);
}
return $params;
}
/**
* Converts given timestamp to the date-time array or given date-time array to the timestamp.
*
* @static static
* @access public
* @param array|int Timestamp or the date-time array
* @return array|int|bool
*/
function convert($mixed)
{
static $monthDays = null;
static $pastMonthsDays = null;
if (!$monthDays)
{
$monthDays = array(
array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
$pastMonthsDays = array(
array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334),
array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335));
}
if (is_array($mixed))
{
$mixed = array_merge(array('year' => 1970, 'month' => 1, 'day' => 1, 'hour' =>0, 'minute' => 0, 'second' => 0), $mixed);
if ($mixed['year'] < 1970 || $mixed['year'] > 2038)
return false;
$isLeap = !($mixed['year'] % 4) && (($mixed['year'] % 100) || !($mixed['year'] % 400));
--$mixed['year'];
--$mixed['month'];
--$mixed['day'];
if ($mixed['month'] < 0 || $mixed['month'] > 11)
return false;
if ($mixed['day'] < 0 || $mixed['day'] >= $monthDays[$isLeap][$mixed['month']])
return false;
if ($mixed['hour'] < 0 || $mixed['hour'] > 23)
return false;
if ($mixed['minute'] < 0 || $mixed['minute'] > 59)
return false;
if ($mixed['second'] < 0 || $mixed['second'] > 59)
return false;
$mixed = ($mixed['day'] + $pastMonthsDays[$isLeap][$mixed['month']] + $mixed['year'] * 365 +
(int)($mixed['year'] / 4) - (int)($mixed['year'] / 100) + (int)($mixed['year'] / 400) - 719162) * 86400 +
$mixed['hour'] * 3600 + $mixed['minute'] * 60 + $mixed['second'];
if ($mixed < 0 || $mixed > 2147483647)
return false;
}
else
{
if ($mixed < 0 || $mixed > 2147483647)
return false;
$datetime = array();
$daycount = (int) ($mixed / 86400);
$dayclock = ($mixed % 86400);
$weekday = ($daycount + 3) % 7 + 1;
$isLeap = 0;
$temptime = (int) ($daycount / 1461);
$daycount = ($daycount % 1461);
$temptime = ($temptime * 4) + 70;
if ($daycount >= 365)
{
$temptime++;
$daycount -= 365;
if ($daycount >= 365)
{
$temptime++;
$daycount -= 365;
if ($daycount >= 366)
{
$temptime++;
$daycount -= 366;
}
else
{
$isLeap++;
}
}
}
for ($month = 11; $daycount < $pastMonthsDays[$isLeap][$month]; --$month);
$mixed = array(
'timestamp' => $mixed,
'year' => 1900 + $temptime,
'month' => $month+1,
'day' => $daycount - $pastMonthsDays[$isLeap][$month] + 1,
'hour' => (int) ($dayclock / 3600),
'minute' => (int) (($dayclock % 3600) / 60),
'second' => $dayclock % 60,
'daysInYear' => 365 + $isLeap,
'daysInMonth' => $monthDays[$isLeap][$month],
'dayOfYear' => $daycount,
'dayOfWeek' => $weekday,
);
}
return $mixed;
}
/**
* Returns timestamp of a given date-time information.
*
* @static static
* @access public
* @param int year Year
* @param int month Month
* @param int day Day
* @param int hour Hour
* @param int minute Minute
* @param int second Second
* @return int|bool
*/
function timestamp($year, $month, $day, $hour, $minute, $second)
{
return TTime::convert(
array(
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $second));
}
/**
* Returns associative array containing date-time information of a given timestamp.
*
* $result['timestamp'] - given timestamp.
* $result['year'] - year.
* $result['month'] - month.
* $result['hour'] - hour.
* $result['minute'] - minute.
* $result['second'] - second.
* $result['daysInYear'] - number of days in the year.
* $result['daysInMonth'] - number of days in the month.
* $result['dayOfYear'] - numeric representation of the day of the year.
* $result['dayOfWeek'] - numeric representation of the day of the week (Monday = 0).
*
* @static static
* @access public
* @param int timestamp Timestamp
* @return array|bool
*/
function datetime($timestamp)
{
return TTime::convert($timestamp);
}
/**
* Returns the current local timestamp.
*
* @static static
* @access public
* @return int
*/
function localTimestamp()
{
return time();
}
/**
* Returns the current UTC (Universal time) timestamp.
*
* @static static
* @access public
* @return int|bool
*/
function universalTimestamp()
{
$params = TTime::temezone();
$timestamp = time() - $params['timezone'] - $params['dstime'];
if ($timestamp < 0 || $timestamp > 2147483647)
return false;
return $timestamp;
}
}
?>