开发者

strtotime() for a date that isn't in a standard form

I have a string in php that is a date but is formatted: mddyy The strtotime() function does not understand this. I know I could split it into an array and move s开发者_如何学编程tuff around as needed to get where I need but is there something that would be better? What is a good way to convert this to a php date?

The format my dates are in are variable length(could be 5 digits, could be 6) depending on the month.

12511 = January 25 2011
110511 = November 5 2011


use substr to split it out...

$date='72811';
if (strlen($date)==5) {
    // 5 digit dates
    $unixtime = mktime(0,0,0, substr(0,1,$date), substr(1,2,$date), substr(3,2,$date));
} else {
    // 6 digit dates
    $unixtime = mktime(0,0,0, substr(0,2,$date), substr(2,2,$date), substr(4,2,$date));
}


I was able to get date_create_from_format to work by padding a 0 on the left.

date_create_from_format("mjy", str_pad($date, 6, "0", STR_PAD_LEFT));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜