PHP毫秒级时间戳和日期格式转换 方法实现

在并发量搞得情况下,需要开启毫秒级运算!

 

mysql 支持:

`create_time` datetime(3) DEFAULT NULL COMMENT '创建时间',

 

效果:mysql创建时间

 

PHP代码实现:

<?php

    $a =  get_msectime();
    $b = get_microtime_format($a*0.001);
    $c = get_data_format($b);


    echo $a;
    echo "<pre>";
    echo $b;
    echo "<pre>";
    echo $c;



    //返回当前的毫秒时间戳
    function get_msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
 
     }
     
     /** 
      *时间戳 转   日期格式 : 精确到毫秒,x代表毫秒
      */
     function get_microtime_format($time)
     {  
        if(strstr($time,'.')){
            sprintf("%01.3f",$time); //小数点。不足三位补0
            list($usec, $sec) = explode(".",$time);
            $sec = str_pad($sec,3,"0",STR_PAD_RIGHT); //不足3位。右边补0
        }else{
            $usec = $time;
            $sec = "000"; 
        }
        $date = date("Y-m-d H:i:s.x",$usec);
        return str_replace('x', $sec, $date);
     }

      /** 时间日期转时间戳格式,精确到毫秒,
      *     
      */
     function get_data_format($time)
     {
        list($usec, $sec) = explode(".", $time);
        $date = strtotime($usec);
        $return_data = str_pad($date.$sec,13,"0",STR_PAD_RIGHT); //不足13位。右边补0
        return $return_data;
     }

 

最终效果:

PHP毫秒级时间戳

    A+
发布日期:2018年02月21日 15:09:10  所属分类:PHP
最后更新时间:2018-02-21 15:10:19
头像
  • ¥ 15.0元
  • 市场价:15.0元
  • ¥ 98.0元
  • 市场价:298.0元
  • ¥ 45.0元
  • 市场价:45.0元
  • ¥ 388.0元
  • 市场价:388.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: