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

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

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

/** 获取当前时间戳,精确到毫秒 */
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] <p>
 * 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.
 * </p>
 * <p>
 * If the optional get_as_float is set to
 * true then a float (in seconds) is returned.
 * </p>
 * @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<p> t2 = $t2 <p> t3 = $t3 <p>";
echo "tInt = $tInt <p>";

输出:

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容