Home  >  Article  >  Backend Development  >  How to convert php date to timestamp milliseconds

How to convert php date to timestamp milliseconds

藏色散人
藏色散人Original
2021-06-19 10:12:463600browse

How to convert php date to timestamp milliseconds: First create a PHP sample file; then use the "function get_data_format($time){...}" method to convert the time and date into timestamp format, and be accurate to Just milliseconds.

How to convert php date to timestamp milliseconds

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to convert php date to timestamp milliseconds?

PHP millisecond-level timestamp and date format conversion method implementation

When the amount of concurrency is high, millisecond-level operations need to be enabled!

mysql Support:

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

Effect:

How to convert php date to timestamp milliseconds

PHP code implementation:

<?php
    $a =  get_msectime();
    $b = get_microtime_format($a*0.001);
    $c = get_data_format($b);
    echo $a;
    echo "<pre class="brush:php;toolbar:false">";
    echo $b;
    echo "<pre class="brush:php;toolbar:false">";
    echo $c;
    //返回当前的毫秒时间戳
    function get_msectime() {
        list($msec, $sec) = explode(&#39; &#39;, microtime());
        $msectime =  (float)sprintf(&#39;%.0f&#39;, (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
 
     }
     
     /** 
      *时间戳 转   日期格式 : 精确到毫秒,x代表毫秒
      */
     function get_microtime_format($time)
     {  
        if(strstr($time,&#39;.&#39;)){
            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(&#39;x&#39;, $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;
     }

Final effect:

How to convert php date to timestamp milliseconds

## Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to convert php date to timestamp milliseconds. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn