php microtime() format value


php microtime() format value

PHP的microtime()返回如下内容:

0.56876200 1385731177 //that's msec sec

这个值我需要它的格式:

1385731177056876200 //this is sec msec without space and dot

目前我正在做这样的事情:

$microtime =  microtime();
$microtime_array = explode(" ", $microtime);
$value = $microtime_array[1] . str_replace(".", "", $microtime_array[0]);

有没有一行代码可以实现这一点?

您可以使用regex:在一行中完成整个操作

$value = preg_replace('/(0)'.('d+) ('d+)/', '$3$1$2', microtime());

示例

<?php
    $microtime = microtime();
    var_dump( $microtime );
    var_dump( preg_replace('/(0)'.('d+) ('d+)/', '$3$1$2', $microtime) );
?>

输出

string(21) "0.49323800 1385734417"  
string(19) "1385734417049323800"

DEMO

不幸的是,由于PHP对浮点表示的限制(直到整个数字为14位),使用microtime()true作为参数没有什么意义。

因此,您必须像处理字符串一样处理它(例如,通过preg_replace()),或者调整precision以使用本机函数调用:

var_dump(1234567.123456789);//float(1234567.1234568)
ini_set('precision', 16);
var_dump(1234567.123456789);//float(1234567.123456789)

-所以,它会像:

ini_set('precision', 20);
var_dump(str_replace('.', '', microtime(1)));//string(20) "13856484375004820824" 

-仍然不是"一行",但您知道导致这种行为的原因,所以您可以只调整precision一次,然后使用它。