<?php
// 获取毫秒时间戳的示例代码
// 方法一:使用 microtime() 函数
function getMillisecondTimestamp1() {
// microtime(true) 返回一个浮点数,表示自 Unix 纪元以来的秒数(包含小数部分)
$timestamp = microtime(true);
// 将浮点数乘以 1000 转换为毫秒,并去掉小数部分
$millisecondTimestamp = floor($timestamp * 1000);
return $millisecondTimestamp;
}
// 方法二:使用 hrtime() 函数(PHP 7.3+)
if (function_exists('hrtime')) {
function getMillisecondTimestamp2() {
// hrtime(true) 返回纳秒级的时间,将其转换为毫秒
$nanoseconds = hrtime(true);
$millisecondTimestamp = floor($nanoseconds / 1e6);
return $millisecondTimestamp;
}
} else {
echo "hrtime() is not available in your PHP version.";
}
// 测试输出
echo getMillisecondTimestamp1() . "\n";
if (function_exists('getMillisecondTimestamp2')) {
echo getMillisecondTimestamp2() . "\n";
}
?>
microtime(true) 函数获取当前时间的浮点数表示形式(秒数,包含小数部分),然后将其乘以 1000 转换为毫秒,并去掉小数部分。hrtime(true) 函数(仅在 PHP 7.3 及以上版本可用)获取当前时间的纳秒级时间戳,然后将其转换为毫秒。你可以根据你的 PHP 版本选择合适的方法来获取毫秒时间戳。
上一篇:php hash_hmac
下一篇:php 循环
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站