关于编码时对时间操作的思考

栏目: PHP · 发布时间: 5年前

内容简介:几乎所有的程序员都会接触到对时间的处理,根据与前端的交互,我将时间分为两类:整型(时间戳)和字符串(各种日期格式)。总而言之,在常规的开发过程里,经常会需要对时间进行处理。我会无脑的将所有的时间都处理成时间戳,然后就可以进行时间比较和各种转化。核心代码如下,可以根据项目情况酌情再改其中的字符串匹配。

前言

几乎所有的 程序员 都会接触到对时间的处理,根据与前端的交互,我将时间分为两类:整型(时间戳)和字符串(各种日期格式)。

时间戳与时区

  • unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数。
    • 所以时间戳是不含时区特性的,同一时间,全球任何地方的时间用时间戳表达应该是一致的。
  • 各种格式的字符串则是各地各状况,一般显示时虽然不含时区标记,也默认为当地时区。

让事情简单点

总而言之,在常规的开发过程里,经常会需要对时间进行处理。我会无脑的将所有的时间都处理成时间戳,然后就可以进行时间比较和各种转化。

核心代码如下,可以根据项目情况酌情再改其中的字符串匹配。

JS:

getTimestamp:function(pTime=null) {
        if (!pTime || pTime=='time()') {
            pTime = parseInt((new Date()).getTime()/1000);
        }
        var time = null;
        if (parseInt(pTime) == pTime)
        {
            time = parseInt(pTime);
        }
        else if (typeof pTime==='string')
        {
            var tmp_datetime = pTime.replace(/[: ]/g,'-');
            tmp_datetime = tmp_datetime.replace(/ /g,'-');
            var arr = tmp_datetime.split("-");
            if (/\d\d\d\d-\d+-\d+ \d+:\d+:\d+/.test(pTime))
            {
                var now = Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]);
                time = parseInt(now/1000);
            }
            else if (/\d\d\d\d-\d+-\d+/.test(pTime))
            {
                var now = Date.UTC(arr[0],arr[1]-1,arr[2],0,0,0);
                time = parseInt(now/1000);
            }
        }
        else if (pTime instanceof Date)
        {
            time = parseInt(pTime.getTime()/1000);
        }
        return time;
    }

PHP:

public static function strtotime($p_time=null)
    {
        if ($p_time===null || $p_time=='time()')
        {
            return time();
        }
        else
        {
            $time = null;
            if (static::is_int($p_time) && strlen($p_time)==13 )
            {
                $p_time = substr($p_time,0,10);
            }
            $p_time = preg_replace_callback('/^(\d{4})[^\d]*?(\d\d)[^\d]*?(\d\d)[^\d]*?(\d\d)[^\d]*?(\d\d)[^\d]*?(\d\d)$/',function($matches){
                if (
                       $matches[1]>=1970 && $matches[1]<=2999
                    && $matches[2]>=1 && $matches[2]<=12
                    && $matches[3]>=1 && $matches[3]<=31
                    && $matches[4]>=0 && $matches[4]<=24
                    && $matches[5]>=0 && $matches[5]<=60
                    && $matches[6]>=0 && $matches[6]<=60
                )
                {
                    return $matches[1] . '-' . $matches[2] . '-' . $matches[3]  . ' ' . $matches[4] . ':' . $matches[5]. ':' . $matches[6] ;
                }
                return $matches[0];
            },$p_time);
            $p_time = preg_replace_callback('/^(\d{4})[^\d]*?(\d\d)[^\d]*?(\d\d)$/',function($matches){
                if (
                       $matches[1]>=1970 && $matches[1]<=2999
                    && $matches[2]>=1 && $matches[2]<=12
                    && $matches[3]>=1 && $matches[3]<=31
                )
                {
                    return $matches[1] . '-' . $matches[2] . '-' . $matches[3];
                }
                return $matches[0];
            },$p_time);
            $strtotime = strtotime($p_time);
            if (static::is_int($p_time) )
            {
                if ($p_time>=19700101 && $p_time<=20991231 && strtotime(date('Y-m-d H:i:s',$strtotime)) == $strtotime)
                {//1970 - 2099
                    $time = $strtotime;
                }
                else
                {
                    $time = intval($p_time);
                }
            }
            else if (strtotime(date('Y-m-d H:i:s',$strtotime)) == $strtotime )
            {
                $time = $strtotime;
            }
            else if (is_subclass_of($p_time,'DateTime'))
            {
                $time = $p_time->getTimestamp();
            }
            else
            {
                return null;
            }
            return $time;
        }
    }

后语

本文是一个思路片段的总结,其中代码并不适用于所有情况,仅记录供参考。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web 2.0界面设计模式

Web 2.0界面设计模式

黄玮 / 电子工业出版社 / 2013-9-1 / 59

本书集Web 2.0的发展及特点、Web 2.0界面设计模式基本理论、实际模式实践及代码实现等诸多内容于一身,具有很强的实用性。这些内容不是简单的顺序堆砌,而是以Web 2.0界面设计模式和应用为主线,其中完美地穿插了各种与之相关的Web 2.0设计理念、用户行为模式、用户体验及基于Dojo的实现方式等相关知识,真正做到将Web 2.0界面设计模式所需要的方方面面的知识有机地融为一个整体。实现不需......一起来看看 《Web 2.0界面设计模式》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具