php time 字符串,php 解析时间字符串

/**

* 解析时间字符串

* @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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值