例如:
$date = date_parse('2010-09-17 14:00:00');
得到我:
year => 2010
month => 9
day => 17
hour => 14
minute => 0
second => 0
根据PHP解释字符串的方式,对应于以下格式:
Y-m-d H:i:s
给定任何日期字符串,如何获取格式字符串,如date_parse / strtotime所理解的那样?
答案 0 :(得分:4)
date("Y-m-d H:i:s", strtotime('2010-09-17 14:00:00'));
我在StackOverflow中的answer中看到了:
不,没有这样的功能来猜测日期格式。您需要编写一组表达所有可能变体的正则表达式匹配,例如
$variations = array (
'^([0-9]{4})/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{4}2$' => 'Y/m/d H:i:s',
// more here
);
foreach ($dateFromDatabase as $date) {
foreach ($variations as $regexp => $dateFormat) {
if (preg_match ('|' . $regexp . '|', $date)) {
$matches[$dateFromDatabase] = $dateFormat;
break;
}
}
}
// $matches now consists of an array of dates => format