博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 解析时间字符串
阅读量:7042 次
发布时间:2019-06-28

本文共 4270 字,大约阅读时间需要 14 分钟。

hot3.png

/**     *  解析时间字符串     * @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);    }

转载于:https://my.oschina.net/xiaoyangok/blog/138410

你可能感兴趣的文章
APP免邀请码能够统计用户安装数据吗?
查看>>
建筑企业施工现场推行5S管理的基本原则
查看>>
Java 枚举 switch的用法
查看>>
程序员编程10大原则,请牢牢记住!
查看>>
这五大方向值得关注 2019智能家居趋势面面观
查看>>
人工智能不是人的智能:先人工,再智能
查看>>
centos 基础性命令
查看>>
Java 8 中处理集合的优雅姿势——Stream
查看>>
mongodb配置---副本集
查看>>
UI设计的几个基本的设计原则
查看>>
远程管理 KVM 虚机 - 每天5分钟玩转 OpenStack(5)
查看>>
python实战系列之通过libvirt操作KVM(六)
查看>>
高可用术语解释:Split-Brain
查看>>
Laravel 队列系统
查看>>
自动FTP 登录备份脚本
查看>>
view
查看>>
我的友情链接
查看>>
tomcat配置文件server.xml详解
查看>>
ipython的两种安装方式
查看>>
有流媒体功能的lnmp部署练习,强化练习
查看>>