/** * 解析时间字符串 * @param $time * @return string */ public static function parseDateTime($time) { if (empty($time)) { return array(); } $formattime = null; //格式化后的时间 $dateTime = null; //匹配的时间 if (preg_match("/(\d{2,4})-(\d{1,2})-(\d{1,2})\s+(上午|下午|am|pm){0,1}(\d{1,2}):(\d{1,2})(:\d{1,2}){0,1}/", $time, $matches)) { //2012 - 9 - 12 14:47:10 //没有秒 if (!empty($matches[7])) { $formattime = "$matches[1]-$matches[2]-$matches[3] $matches[4]$matches[5]:$matches[6]:$matches[7]"; } else { $formattime = "$matches[1]-$matches[2]-$matches[3] $matches[4]$matches[5]:$matches[6]:s"; } if (empty($matches[4])) { $dateTime = date("Y-m-d H:i:s", strtotime($matches[0])); } else { if ($matches[1] == 'am' || $matches[1] == '上午') { $dataString = str_replace(array('am', '上午'), ' ', $matches[0]); $dateTime = date("Y-m-d H:i:s", strtotime($dataString)); } else { $dataString = str_replace(array('pm', '下午'), ' ', $matches[0]); $hours = (int)date("H", strtotime($dataString)); //如果为下午 并且小时超过12则不加12小时 if ($hours >= 12) { $dateTime = date("Y-m-d H:i:s", strtotime($dataString)); } else { $dateTime = date("Y-m-d H:i:s", strtotime($dataString . " +12hours")); } } } } else if (preg_match("/\d{2,4}-\d{1,2}-\d{1,2}/", $time, $matches)) { //2012-7-29 $formattime = $matches[0] . " H:i:s"; $dateTime = date("Y-m-d H:i:s", strtotime($matches[0])); } else if (preg_match("/(\d{4})年(\d{1,2})月(\d{1,2})日(\d{1,2})时(\d{1,2})分(\d{1,2})秒/", $time, $matches)) { // 2013年1月1日12时12分12秒 $dataString = "$matches[1]-$matches[2]-$matches[3] $matches[4]:$matches[5]:$matches[6]"; $dateTime = date("Y-m-d H:i:s", strtotime($dataString)); $formattime = $dateTime; } else if (preg_match("/(\d{4})年(\d{1,2})月(\d{1,2})日 (\d{1,2}):(\d{1,2})/", $time, $matches)) { //2012年12月10日 02:51 $dataString = "$matches[1]-$matches[2]-$matches[3] $matches[4]:$matches[5]:00"; $dateTime = date("Y-m-d H:i:s", strtotime($dataString)); $formattime = "$matches[1]-$matches[2]-$matches[3] $matches[4]:$matches[5]:s"; } else if (preg_match("/(\d{4})年(\d{1,2})月(\d{1,2})日/", $time, $matches)) { //2012年7月29日 $dataString = $matches[0]; $formattime = "$matches[1]-$matches[2]-$matches[3] H:i:s"; $dataString = str_replace(array('年', '月', '日'), array("-", "-", ""), $dataString); $dateTime = date("Y-m-d H:i:s", strtotime($dataString)); } else if (preg_match("/\d{1,2}\s*小时前/", $time, $matches)) { //15 小时前 $hours = (int)($matches[0]); $strtotime = strtotime("- $hours hours"); $dateTime = date("Y-m-d H:i:s", $strtotime); $formattime = date("Y-m-d H", $strtotime) . ":i:s"; } else if (preg_match("/\d{1,2}\s*分钟前/", $time, $matches)) { //36 分钟前 $hours = (int)($matches[0]); $strtotime = strtotime("- $hours minutes"); $dateTime = date("Y-m-d H:i:s", $strtotime); $formattime = date("Y-m-d H:i", $strtotime) . ":s"; } else if (preg_match("/\d{1,2}\s*秒前/", $time, $matches)) { // 27秒前 $hours = (int)($matches[0]); $strtotime = strtotime("- $hours seconds"); $dateTime = date("Y-m-d H:i:s", $strtotime); $formattime = $dateTime; } else if (preg_match("/\d{1,2}-\d{1,2}/", $time, $matches)) { //9-12 $dateTime = date("Y-m-d H:i:s", strtotime(date('y') . '-' . $matches[0])); $formattime = "Y-" . $matches[0] . " H:i:s"; } else if (preg_match("/昨天/", $time, $matches)) { //昨天 $strtotime = strtotime("- 1 day"); $dateTime = date("Y-m-d H:i:s", $strtotime); $formattime = date("Y-m-d", $strtotime) . " H:i:s"; } return array('datetime' => $dateTime, 'formattime' => $formattime); }