别被忽悠,php是有毫秒时间戳的!

今天要用在php里面记录一个毫秒时间戳。
从网上找了一下,很多人都说php没有毫秒时间戳

  • PHP时间戳与时间相互转换(精确到毫秒)
  • PHP获取毫秒级时间戳
  • php利用microtime函数来获取当前毫秒时间戳

大体都用这样的方法取时间戳:

/** 获取当前时间戳,精确到毫秒 */
function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

php真的这么弱???我不信!!!
研究了一番,其实就在microtime里面!!!

/**
 * Return current Unix timestamp with microseconds
 * @link http://php.net/manual/en/function.microtime.php
 * @param bool $get_as_float [optional] 

* When called without the optional argument, this function returns the string * "msec sec" where sec is the current time measured in the number of * seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and * msec is the microseconds part. * Both portions of the string are returned in units of seconds. *

*

* If the optional get_as_float is set to * true then a float (in seconds) is returned. *

* @return mixed * @since 4.0 * @since 5.0 */ function microtime ($get_as_float = null) {}

关键 传递参数get_as_float = true:

If the optional get_as_float is set to true then a float (in seconds) is returned.
如果参数get_as_float被设置成true,将返回float(单位秒)。

When called without the optional argument, 
this function returns the string "msec sec" 
where sec is the current time measured in the number of seconds 
since the Unix Epoch (0:00:00 January 1, 1970 GMT),
and msec is the microseconds part.
当不带参数调用的时候,函数返回一个“毫秒 秒”的字符串,
其中秒是从1979年1月1日0时到现在的秒数,另一部分是毫秒。

动手写个测试很快的:

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$t1 = microtime_float();
$t2 = microtime(false);
$t3 = microtime(true);
$tInt = (int)(microtime(true)*1000);
echo "t1 = $t1

t2 = $t2

t3 = $t3

"; echo "tInt = $tInt

";

输出:

t1 = 1486474356.4957 
t2 = 0.49571200 1486474356 
t3 = 1486474356.4957
tInt = 1486474356495

看来只会用百度的程序员不是好领导。
不要停止质疑和探寻。

你可能感兴趣的:(别被忽悠,php是有毫秒时间戳的!)