CDateTimeParser
Package | system.utils |
---|---|
Inheritance | class CDateTimeParser |
Since | 1.0 |
Source Code | framework/utils/CDateTimeParser.php |
CDateTimeParser converts a date/time string to a UNIX timestamp according to the specified pattern.
The following pattern characters are recognized:
For example, to parse a date string '21/10/2008', use the following:
Locale specific patterns such as MMM and MMMM uses CLocale for retrieving needed information.
To format a timestamp to a date string, please use CDateFormatter.
The following pattern characters are recognized:
Pattern | Description ---------------------------------------------------- d | Day of month 1 to 31, no padding dd | Day of month 01 to 31, zero leading M | Month digit 1 to 12, no padding MM | Month digit 01 to 12, zero leading MMM | Abbreviation representation of month (available since 1.1.11; locale aware since 1.1.13) MMMM | Full name representation (available since 1.1.13; locale aware) y | 4 year digit, e.g., 2005 (available since 1.1.16) yy | 2 year digit, e.g., 96, 05 yyyy | 4 year digit, e.g., 2005 h | Hour in 0 to 12, no padding hh | Hour in 00 to 12, zero leading H | Hour in 0 to 23, no padding HH | Hour in 00 to 23, zero leading m | Minutes in 0 to 59, no padding mm | Minutes in 00 to 59, zero leading s | Seconds in 0 to 59, no padding ss | Seconds in 00 to 59, zero leading a | AM or PM, case-insensitive (since version 1.1.5) ? | matches any character (wildcard) (since version 1.1.11) ----------------------------------------------------All other characters must appear in the date string at the corresponding positions.
For example, to parse a date string '21/10/2008', use the following:
$timestamp=CDateTimeParser::parse('21/10/2008','dd/MM/yyyy');
Locale specific patterns such as MMM and MMMM uses CLocale for retrieving needed information.
To format a timestamp to a date string, please use CDateFormatter.
Public Methods
Method | Description | Defined By |
---|---|---|
parse() | Converts a date string to a timestamp. | CDateTimeParser |
Protected Methods
Method | Description | Defined By |
---|---|---|
parseAmPm() | CDateTimeParser | |
parseInteger() | CDateTimeParser | |
parseMonth() | CDateTimeParser |
Method Details
parse() method
public static integer parse(string $value, string $pattern='MM/dd/yyyy', array $defaults=array ( )) | ||
$value | string | the date string to be parsed |
$pattern | string | the pattern that the date string is following |
$defaults | array | the default values for year, month, day, hour, minute and second. The default values will be used in case when the pattern doesn't specify the corresponding fields. For example, if the pattern is 'MM/dd/yyyy' and this parameter is array('minute'=>0, 'second'=>0), then the actual minute and second for the parsing result will take value 0, while the actual hour value will be the current hour obtained by date('H'). This parameter has been available since version 1.1.5. |
{return} | integer | timestamp for the date string. False if parsing fails. |
Source Code: framework/utils/CDateTimeParser.php#81 (show)
public static function parse($value,$pattern='MM/dd/yyyy',$defaults=array())
{
if(self::$_mbstringAvailable===null)
self::$_mbstringAvailable=extension_loaded('mbstring');
$tokens=self::tokenize($pattern);
$i=0;
$n=self::$_mbstringAvailable ? mb_strlen($value,Yii::app()->charset) : strlen($value);
foreach($tokens as $token)
{
switch($token)
{
case 'yyyy':
case 'y':
{
if(($year=self::parseInteger($value,$i,4,4))===false)
return false;
$i+=4;
break;
}
case 'yy':
{
if(($year=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($year);
break;
}
case 'MMMM':
{
$monthName='';
if(($month=self::parseMonth($value,$i,'wide',$monthName))===false)
return false;
$i+=self::$_mbstringAvailable ? mb_strlen($monthName,Yii::app()->charset) : strlen($monthName);
break;
}
case 'MMM':
{
$monthName='';
if(($month=self::parseMonth($value,$i,'abbreviated',$monthName))===false)
return false;
$i+=self::$_mbstringAvailable ? mb_strlen($monthName,Yii::app()->charset) : strlen($monthName);
break;
}
case 'MM':
{
if(($month=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'M':
{
if(($month=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($month);
break;
}
case 'dd':
{
if(($day=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'd':
{
if(($day=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($day);
break;
}
case 'h':
case 'H':
{
if(($hour=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($hour);
break;
}
case 'hh':
case 'HH':
{
if(($hour=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'm':
{
if(($minute=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($minute);
break;
}
case 'mm':
{
if(($minute=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 's':
{
if(($second=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($second);
break;
}
case 'ss':
{
if(($second=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'a':
{
if(($ampm=self::parseAmPm($value,$i))===false)
return false;
if(isset($hour))
{
if($hour==12 && $ampm==='am')
$hour=0;
elseif($hour<12 && $ampm==='pm')
$hour+=12;
}
$i+=2;
break;
}
default:
{
$tn=self::$_mbstringAvailable ? mb_strlen($token,Yii::app()->charset) : strlen($token);
if($i>=$n || ($token{0}!='?' && (self::$_mbstringAvailable ? mb_substr($value,$i,$tn,Yii::app()->charset) : substr($value,$i,$tn))!==$token))
return false;
$i+=$tn;
break;
}
}
}
if($i<$n)
return false;
if(!isset($year))
$year=isset($defaults['year']) ? $defaults['year'] : date('Y');
if(!isset($month))
$month=isset($defaults['month']) ? $defaults['month'] : date('n');
if(!isset($day))
$day=isset($defaults['day']) ? $defaults['day'] : date('j');
if(strlen($year)===2)
{
if($year>=70)
$year+=1900;
else
$year+=2000;
}
$year=(int)$year;
$month=(int)$month;
$day=(int)$day;
if(
!isset($hour) && !isset($minute) && !isset($second)
&& !isset($defaults['hour']) && !isset($defaults['minute']) && !isset($defaults['second'])
)
$hour=$minute=$second=0;
else
{
if(!isset($hour))
$hour=isset($defaults['hour']) ? $defaults['hour'] : date('H');
if(!isset($minute))
$minute=isset($defaults['minute']) ? $defaults['minute'] : date('i');
if(!isset($second))
$second=isset($defaults['second']) ? $defaults['second'] : date('s');
$hour=(int)$hour;
$minute=(int)$minute;
$second=(int)$second;
}
if(CTimestamp::isValidDate($year,$month,$day) && CTimestamp::isValidTime($hour,$minute,$second))
return CTimestamp::getTimestamp($hour,$minute,$second,$month,$day,$year);
else
return false;
}
Converts a date string to a timestamp.
parseAmPm() method
protected static string parseAmPm(string $value, integer $offset) | ||
$value | string | the date string to be parsed |
$offset | integer | starting offset |
{return} | string | parsed day period value |
Source Code: framework/utils/CDateTimeParser.php#312 (show)
protected static function parseAmPm($value, $offset)
{
$v=strtolower(self::$_mbstringAvailable ? mb_substr($value,$offset,2,Yii::app()->charset) : substr($value,$offset,2));
return $v==='am' || $v==='pm' ? $v : false;
}
parseInteger() method
protected static string parseInteger(string $value, integer $offset, integer $minLength, integer $maxLength) | ||
$value | string | the date string to be parsed |
$offset | integer | starting offset |
$minLength | integer | minimum length |
$maxLength | integer | maximum length |
{return} | string | parsed integer value |
Source Code: framework/utils/CDateTimeParser.php#296 (show)
protected static function parseInteger($value,$offset,$minLength,$maxLength)
{
for($len=$maxLength;$len>=$minLength;--$len)
{
$v=self::$_mbstringAvailable ? mb_substr($value,$offset,$len,Yii::app()->charset) : substr($value,$offset,$len);
if(ctype_digit($v) && (self::$_mbstringAvailable ? mb_strlen($v,Yii::app()->charset) : strlen($v))>=$minLength)
return $v;
}
return false;
}
parseMonth() method (available since v1.1.13)
protected static string parseMonth(string $value, integer $offset, string $width, string &$monthName) | ||
$value | string | the date string to be parsed. |
$offset | integer | starting offset. |
$width | string | month name width. It can be 'wide', 'abbreviated' or 'narrow'. |
$monthName | string | extracted month name. Passed by reference. |
{return} | string | parsed month name. |
Source Code: framework/utils/CDateTimeParser.php#326 (show)
protected static function parseMonth($value,$offset,$width,&$monthName)
{
$valueLength=self::$_mbstringAvailable ? mb_strlen($value,Yii::app()->charset) : strlen($value);
for($len=1; $offset+$len<=$valueLength; $len++)
{
$monthName=self::$_mbstringAvailable ? mb_substr($value,$offset,$len,Yii::app()->charset) : substr($value,$offset,$len);
if(!preg_match('/^[\p{L}\p{M}]+$/u',$monthName)) // unicode aware replacement for ctype_alpha($monthName)
{
$monthName=self::$_mbstringAvailable ? mb_substr($monthName,0,-1,Yii::app()->charset) : substr($monthName,0,-1);
break;
}
}
$monthName=self::$_mbstringAvailable ? mb_strtolower($monthName,Yii::app()->charset) : strtolower($monthName);
$monthNames=Yii::app()->getLocale()->getMonthNames($width,false);
foreach($monthNames as $k=>$v)
$monthNames[$k]=rtrim(self::$_mbstringAvailable ? mb_strtolower($v,Yii::app()->charset) : strtolower($v),'.');
$monthNamesStandAlone=Yii::app()->getLocale()->getMonthNames($width,true);
foreach($monthNamesStandAlone as $k=>$v)
$monthNamesStandAlone[$k]=rtrim(self::$_mbstringAvailable ? mb_strtolower($v,Yii::app()->charset) : strtolower($v),'.');
if(($v=array_search($monthName,$monthNames))===false && ($v=array_search($monthName,$monthNamesStandAlone))===false)
return false;
return $v;
}
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CDateTimeParser