Time()是以秒为单位的——有以毫秒为单位的吗?
当前回答
在PHP 5中使用microtime(true),或者在PHP 4中进行以下修改:
array_sum(explode(' ', microtime()));
编写该代码的可移植方法是:
function getMicrotime()
{
if (version_compare(PHP_VERSION, '5.0.0', '<'))
{
return array_sum(explode(' ', microtime()));
}
return microtime(true);
}
其他回答
如果你想看到真正的微秒,你需要将php.ini中的精度设置更改为16。
在此之后,microsecond(true)为我提供了1631882476.298437的输出。
所以我认为我需要用余数(298437)除以1000,但实际上,余数是0.298437秒。所以我需要把它乘以1000才能得到正确的结果。
function get_milliseconds()
{
$timestamp = microtime(true);
return (int)(($timestamp - (int)$timestamp) * 1000);
}
使用microtime。该函数返回一个以空格分隔的字符串。第一部分是秒的小数部分,第二部分是积分部分。传入true以获得一个数字:
var_dump(microtime()); // string(21) "0.89115400 1283846202"
var_dump(microtime(true)); // float(1283846202.89)
如果使用微时(true),请注意精度损失。
还有gettimeofday,它以整数形式返回微秒部分。
var_dump(gettimeofday());
/*
array(4) {
["sec"]=>
int(1283846202)
["usec"]=>
int(891199)
["minuteswest"]=>
int(-60)
["dsttime"]=>
int(1)
}
*/
echo date('Y-m-d H:i:s.')。gettimeofday()(“购买”);
输出:
2016-11-19 15:12:34.346351
$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
($the_date_time->format('u') / 1000);
这是我的实现,应该工作在32位以及。
function mstime(){
$mstime = explode(' ',microtime());
return $mstime[1].''.(int)($mstime[0]*1000);
}
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 如何解析unix时间戳到时间。时间