CDateTimeParser converts a date/time string to a UNIX timestamp according to the specified pattern.
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:
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.
public static function parse($value,$pattern='MM/dd/yyyy',$defaults=array()) { if(self::$_mbstringAvailable===null) self::$_mbstringAvailable=extension_loaded('mbstring');
fix CDateTimeParser::parse() to handle translated values of 'am' and 'pm'
CDateTimeParser::parse() does not handle translated am/pm values
consider this scenario:
user's locale is 'zh'
construct a date/time with 'short' date and 'short' time, yields something like '2016-5-5 下午3:10' (note the inverted time-format ah:mm where the am/pm prefix is BEFORE the hrs/mins)
now try to parse the generated date/time using parse(). It fails in two ways:
because 下午 is not 'am' nor 'pm'
because of the inverted time format
this patch to CDateTimeParser fixes both problems (works against Yii 1.1.17, maybe against previous versions):
This class solved my problem of getting a timestamp from a datetime object in the mysql database. Retrieve the datetime using your model, then do the following.
Total 2 comments
CDateTimeParser::parse() does not handle translated am/pm values
consider this scenario:
this patch to CDateTimeParser fixes both problems (works against Yii 1.1.17, maybe against previous versions):
This class solved my problem of getting a timestamp from a datetime object in the mysql database. Retrieve the datetime using your model, then do the following.
Leave a comment
Please login to leave your comment.