CTimestamp

优质
小牛编辑
122浏览
2023-12-01
所有包 | 方法
system.utils
继承class CTimestamp
源自1.0
版本$Id: CTimestamp.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/utils/CTimestamp.php
CTimestamp represents a timestamp.

Part of this class was adapted from the ADOdb Date Library ADOdb abstraction library. The original source code was released under both BSD and GNU Lesser GPL library license, with the following copyright notice: Copyright (c) 2000, 2001, 2002, 2003, 2004 John Lim All rights reserved.

This class is provided to support UNIX timestamp that is beyond the range of 1901-2038 on Unix and1970-2038 on Windows. Except getTimestamp, all other methods in this class can work with the extended timestamp range. For getTimestamp, because it is merely a wrapper of http://php.net/manual/en/function.mktime.php, it may still be subject to the limit of timestamp range on certain platforms. Please refer to the PHP manual for more information.

公共方法

隐藏继承方法

方法描述定义在
formatDate()Formats a timestamp to a date string.CTimestamp
get4DigitYear()Returns 4-digit representation of the year.CTimestamp
getDate()Returns the getdate() array.CTimestamp
getDayofWeek()Gets day of week, 0 = Sunday,... 6=Saturday.CTimestamp
getGMTDiff()返回get local time zone offset from GMTCTimestamp
getTimestamp()Generates a timestamp.CTimestamp
isLeapYear()Checks for leap year, returns true if it is. No 2-digit year check. AlsoCTimestamp
isValidDate()Checks to see if the year, month, day are valid combination.CTimestamp
isValidTime()Checks to see if the hour, minute and second are valid.CTimestamp

受保护方法

隐藏继承方法

方法描述定义在
digitCheck()Fix 2-digit years. Works for any century.CTimestamp

方法详细

digitCheck() 方法
protected static integer digitCheck(integer $y)
$yintegeryear
{return}integerchange two digit year into multiple digits
源码: framework/utils/CTimestamp.php#113 (显示) protectedstaticfunctiondigitCheck($y)
{
if($y<100){
$yr=(integer)date("Y");
$century=(integer)($yr/100);

if($yr%100>50){
$c1=$century+1;
$c0=$century;
}else{
$c1=$century;
$c0=$century-1;
}
$c1*=100;
//if2-digityearislessthan30yearsinfuture,setittothiscentury
//otherwiseifmorethan30yearsinfuture,thenweset2-digityeartotheprevcentury.
if(($y+$c1)<$yr+30)$y=$y+$c1;
else$y=$y+$c0*100;
}
return$y;
}

Fix 2-digit years. Works for any century. Assumes that if 2-digit is more than 30 years in future, then previous century.

formatDate() 方法
public static string formatDate(string $fmt, integer|boolean $d=false, boolean $is_gmt=false)
$fmtstringformat pattern
$dinteger|booleantimestamp
$is_gmtbooleanwhether this is a GMT timestamp
{return}stringformatted date based on timestamp $d
源码: framework/utils/CTimestamp.php#217 (显示) publicstaticfunctionformatDate($fmt,$d=false,$is_gmt=false)
{
if($d===false)
return($is_gmt)?@gmdate($fmt):@date($fmt);

//checkifnumberin32-bitsignedrange
if((abs($d)<=0x7FFFFFFF))
{
//ifwindows,mustbe+veinteger
if($d>=0)
return($is_gmt)?@gmdate($fmt,$d):@date($fmt,$d);
}

$_day_power=86400;

$arr=self::getDate($d,true,$is_gmt);

$year=$arr['year'];
$month=$arr['mon'];
$day=$arr['mday'];
$hour=$arr['hours'];
$min=$arr['minutes'];
$secs=$arr['seconds'];

$max=strlen($fmt);
$dates='';

/*
atthispoint,wehavethefollowingintegervarstomanipulate:
$year,$month,$day,$hour,$min,$secs
*/
for($i=0;$i<$max;$i++)
{
switch($fmt[$i])
{
case'T':$dates.=date('T');break;
//YEAR
case'L':$dates.=$arr['leap']?'1':'0';break;
case'r'://Thu,21Dec200016:01:07+0200

//4.3.11uses'04Jun2004'
//4.3.8uses'4Jun2004'
$dates.=gmdate('D',$_day_power*(3+self::getDayOfWeek($year,$month,$day))).','
.($day<10?'0'.$day:$day).''.date('M',mktime(0,0,0,$month,2,1971)).''.$year.'';

if($hour<10)$dates.='0'.$hour;else$dates.=$hour;

if($min<10)$dates.=':0'.$min;else$dates.=':'.$min;

if($secs<10)$dates.=':0'.$secs;else$dates.=':'.$secs;

$gmt=self::getGMTDiff();
$dates.=sprintf('%s%04d',($gmt<=0)?'+':'-',abs($gmt)/36);
break;

case'Y':$dates.=$year;break;
case'y':$dates.=substr($year,strlen($year)-2,2);break;
//MONTH
case'm':if($month<10)$dates.='0'.$month;else$dates.=$month;break;
case'Q':$dates.=($month+3)>>2;break;
case'n':$dates.=$month;break;
case'M':$dates.=date('M',mktime(0,0,0,$month,2,1971));break;
case'F':$dates.=date('F',mktime(0,0,0,$month,2,1971));break;
//DAY
case't':$dates.=$arr['ndays'];break;
case'z':$dates.=$arr['yday'];break;
case'w':$dates.=self::getDayOfWeek($year,$month,$day);break;
case'l':$dates.=gmdate('l',$_day_power*(3+self::getDayOfWeek($year,$month,$day)));break;
case'D':$dates.=gmdate('D',$_day_power*(3+self::getDayOfWeek($year,$month,$day)));break;
case'j':$dates.=$day;break;
case'd':if($day<10)$dates.='0'.$day;else$dates.=$day;break;
case'S':
$d10=$day%10;
if($d10==1)$dates.='st';
elseif($d10==2&&$day!=12)$dates.='nd';
elseif($d10==3)$dates.='rd';
else$dates.='th';
break;

//HOUR
case'Z':
$dates.=($is_gmt)?0:-self::getGMTDiff();break;
case'O':
$gmt=($is_gmt)?0:self::getGMTDiff();

$dates.=sprintf('%s%04d',($gmt<=0)?'+':'-',abs($gmt)/36);
break;

case'H':
if($hour<10)$dates.='0'.$hour;
else$dates.=$hour;
break;
case'h':
if($hour>12)$hh=$hour-12;
else{
if($hour==0)$hh='12';
else$hh=$hour;
}

if($hh<10)$dates.='0'.$hh;
else$dates.=$hh;
break;

case'G':
$dates.=$hour;
break;

case'g':
if($hour>12)$hh=$hour-12;
else{
if($hour==0)$hh='12';
else$hh=$hour;
}
$dates.=$hh;
break;
//MINUTES
case'i':if($min<10)$dates.='0'.$min;else$dates.=$min;break;
//SECONDS
case'U':$dates.=$d;break;
case's':if($secs<10)$dates.='0'.$secs;else$dates.=$secs;break;
//AM/PM
//Note00:00to11:59isAM,while12:00to23:59isPM
case'a':
if($hour>=12)$dates.='pm';
else$dates.='am';
break;
case'A':
if($hour>=12)$dates.='PM';
else$dates.='AM';
break;
default:
$dates.=$fmt[$i];break;
//ESCAPE
case"\":
$i++;
if($i<$max)$dates.=$fmt[$i];
break;
}
}
return$dates;
}

Formats a timestamp to a date string.

get4DigitYear() 方法
public static integer get4DigitYear(integer $y)
$yintegeryear
{return}integer4-digit representation of the year
源码: framework/utils/CTimestamp.php#140 (显示) publicstaticfunctionget4DigitYear($y)
{
returnself::digitCheck($y);
}

Returns 4-digit representation of the year.

getDate() 方法
public static array getDate(integer|boolean $d=false, boolean $fast=false, boolean $gmt=false)
$dinteger|booleanoriginal date timestamp. False to use the current timestamp.
$fastbooleanfalse to compute the day of the week, default is true
$gmtbooleantrue to calculate the GMT dates
{return}arrayan array with date info.
源码: framework/utils/CTimestamp.php#164 (显示) publicstaticfunctiongetDate($d=false,$fast=false,$gmt=false)
{
if($d===false)
$d=time();
if($gmt)
{
$tz=date_default_timezone_get();
date_default_timezone_set('GMT');
$result=getdate($d);
date_default_timezone_set($tz);
}
else
{
$result=getdate($d);
}
return$result;
}

Returns the getdate() array.

getDayofWeek() 方法
public static integer getDayofWeek(integer $year, integer $month, integer $day)
$yearintegeryear
$monthintegermonth
$dayintegerday
{return}integerday of week
源码: framework/utils/CTimestamp.php#44 (显示) publicstaticfunctiongetDayofWeek($year,$month,$day)
{
/*
PopeGregoryremoved10days-October5toOctober14-fromtheyear1582and
proclaimedthatfromthattimeonwards3dayswouldbedroppedfromthecalendar
every400years.

Thursday,October4,1582(Julian)wasfollowedimmediatelybyFriday,October15,1582(Gregorian).
*/
if($year<=1582)
{
if($year<1582||
($year==1582&&($month<10||($month==10&&$day<15))))
{
$greg_correction=3;
}
else
{
$greg_correction=0;
}
}
else
{
$greg_correction=0;
}

if($month>2)
$month-=2;
else
{
$month+=10;
$year--;
}

$day=floor((13*$month-1)/5)+
$day+($year%100)+
floor(($year%100)/4)+
floor(($year/100)/4)-2*
floor($year/100)+77+$greg_correction;

return$day-7*floor($day/7);
}

Gets day of week, 0 = Sunday,... 6=Saturday. Algorithm from PEAR::Date_Calc

getGMTDiff() 方法
public static integer getGMTDiff()
{return}integerget local time zone offset from GMT
源码: framework/utils/CTimestamp.php#148 (显示) publicstaticfunctiongetGMTDiff()
{
static$TZ;
if(isset($TZ))return$TZ;

$TZ=mktime(0,0,0,1,2,1970)-gmmktime(0,0,0,1,2,1970);
return$TZ;
}
getTimestamp() 方法
public static integer|float getTimestamp(integer $hr, integer $min, integer $sec, integer|boolean $mon=false, integer|boolean $day=false, integer|boolean $year=false, boolean $is_gmt=false)
$hrintegerhour
$minintegerminute
$secintegersecond
$moninteger|booleanmonth
$dayinteger|booleanday
$yearinteger|booleanyear
$is_gmtbooleanwhether this is GMT time. If true, gmmktime() will be used.
{return}integer|floata timestamp given a local time.
源码: framework/utils/CTimestamp.php#371 (显示) publicstaticfunctiongetTimestamp($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_gmt=false)
{
if($mon===false)
return$is_gmt?@gmmktime($hr,$min,$sec):@mktime($hr,$min,$sec);
return$is_gmt?@gmmktime($hr,$min,$sec,$mon,$day,$year):@mktime($hr,$min,$sec,$mon,$day,$year);
}

Generates a timestamp. This is the same as the PHP function http://php.net/manual/en/function.mktime.php.

isLeapYear() 方法
public static boolean isLeapYear(integer $year)
$yearintegeryear to check
{return}booleantrue if is leap year
源码: framework/utils/CTimestamp.php#93 (显示) publicstaticfunctionisLeapYear($year)
{
$year=self::digitCheck($year);
if($year%4!=0)
returnfalse;

if($year%400==0)
returntrue;
//ifgregoriancalendar(>1582),centurynot-divisibleby400isnotleap
elseif($year>1582&&$year%100==0)
returnfalse;
returntrue;
}

Checks for leap year, returns true if it is. No 2-digit year check. Also handles julian calendar correctly.

isValidDate() 方法
public static boolean isValidDate(integer $y, integer $m, integer $d)
$yintegeryear
$mintegermonth
$dintegerday
{return}booleantrue if valid date, semantic check only.
源码: framework/utils/CTimestamp.php#189 (显示) publicstaticfunctionisValidDate($y,$m,$d)
{
returncheckdate($m,$d,$y);
}

Checks to see if the year, month, day are valid combination.

isValidTime() 方法
public static boolean isValidTime(integer $h, integer $m, integer $s, boolean $hs24=true)
$hintegerhour
$mintegerminute
$sintegersecond
$hs24booleanwhether the hours should be 0 through 23 (default) or 1 through 12.
{return}booleantrue if valid date, semantic check only.
源码: framework/utils/CTimestamp.php#202 (显示) publicstaticfunctionisValidTime($h,$m,$s,$hs24=true)
{
if($hs24&&($h<0||$h>23)||!$hs24&&($h<1||$h>12))returnfalse;
if($m>59||$m<0)returnfalse;
if($s>59||$s<0)returnfalse;
returntrue;
}

Checks to see if the hour, minute and second are valid.