0

我想将这样的日期 '2012 年 3 月 30 日 9:30 pm' 转换成这种格式 2012-03-30 09:30:00 最简单的方法是什么?我需要它在 PHP 5.2 和更早版本上工作,所以这个解决方案对我没有任何用处

date_format('Y-m-d H:M:S',date_create_from_format('Y m F g:i A', '2012 30 March 9:30 pm'))
4

5 回答 5

1
EDIT: following Stewie advice:

<?php

list($year, $day, $month, $hour, $minute, $ampm) = sscanf("2012 30 March 9:30 pm
", "%d %d %s %d:%d %s");

if($ampm == 'pm') $hour += 12;

echo date("Y-m-d H:i:s", strtotime($day . ' ' . $month . ' ' . $year . ' ' . $ho
ur . ':' . $minute));
于 2012-03-15T13:12:09.960 回答
1

使用 sscanf(),然后将字符串设置为正确的格式,对其使用 strtotime(),然后使用所需格式设置 date()。

于 2012-03-15T13:12:41.470 回答
1

这将从日期中提取每个组件并重新排列它以便strtotime()理解。之后转换为您想要的任何日期格式。

不过,使用正则表达式可能有点矫枉过正。

function parse_date_time_to_ts($dt) 
{
    $dt_pattern = '#([0-9]{4})\s+([0-9]{1,2})\s+([a-z]+)\s+([0-9]{1,2}:[0-9]{2})\s+(am|pm)#is';
    $dt_replace = '$2 $3 $1 $4 $5';

    return strtotime(preg_replace($dt_pattern, $dt_replace, $dt));
}
于 2012-03-15T13:28:38.760 回答
0

http://php.net/manual/en/function.strtotime.php#106043

请参阅官方文档和用户贡献的链接...

于 2012-03-15T13:18:09.233 回答
0

试试这个

list($year, $month, $day, $year, $hour, $minutes, $a) = sscanf("2012 30 March 9:30 pm", "%d %d %d $s $d:$d $s");
echo date("Y-m-d h:i:s", strtotime($day." ".$month." ".$year." ".$hour.":".minutes." ".$a));
于 2012-03-15T13:27:40.527 回答