PHP输出时间精确到毫秒
编辑时间:2022-02-14 作者:劲枫 浏览量:484 来源:原创

php默认时间戳是秒为单位的,js里的是微妙,php怎么和js一样呢?看这里


<?php
/**
 * Created by PhpStorm.
 * User: jefferyteng
 * Date: 2018/8/28
 * Time: 10:00
 */
 
/*1、直接打印微秒0.24754300 1535423356*/
echo(microtime());
echo "<br/>";
 
/*2、十三位时间戳,包含毫秒1535423356248*/
function msectime()
{
    list($msec, $sec) = explode(' ', microtime());
    $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    return $msectime;
}
echo msectime();
echo "<br/>";
 
 
//3、获取当前系统时间并打印
$date = new DateTime();
echo $date->format('Y-m-d H:i:s');
echo "<br/>";
 
 
 
 
/*4、完整的时间:2018-08-28 10:29:16 247591*/
function udate($format = 'u', $utimestamp = null)
{
    if (is_null($utimestamp)){
        $utimestamp = microtime(true);
    }
    $timestamp = floor($utimestamp);
    $milliseconds = round(($utimestamp - $timestamp) * 1000000);//改这里的数值控制毫秒位数
    return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
echo udate('Y-m-d H:i:s u');
echo "<br/>";
 
 
//5、只要毫秒+微秒:247624
function milliseconds($format = 'u', $utimestamp = null)
{
    if (is_null($utimestamp)){
        $utimestamp = microtime(true);
    }
    $timestamp = floor($utimestamp);
    $milliseconds = round(($utimestamp - $timestamp) * 1000000);//改这里的数值控制毫秒位数
    return $milliseconds;
}
echo milliseconds();



文章转自:https://blog.csdn.net/tcf_jingfeng/article/details/82143440

来说两句吧