
最佳答案

1

1

讨论数量:
PHP 获取毫秒级时间戳:
microtime()
// 0.45057400 1590548298
// 结果是分两部分的,也就是前半部分是毫秒(但是单位是秒),后半部分是秒。需要自己处理一下
Carbon 获取指定精度的时间戳:
Carbon::now()->getPreciseTimestamp();
Carbon 源码里有:
/**
* Returns a timestamp rounded with the given precision (6 by default).
*
* @example getPreciseTimestamp() 1532087464437474 (微秒最大精度)
* @example getPreciseTimestamp(6) 1532087464437474
* @example getPreciseTimestamp(5) 153208746443747 (1/100000 秒精度)
* @example getPreciseTimestamp(4) 15320874644375 (1/10000 秒精度)
* @example getPreciseTimestamp(3) 1532087464437 (毫秒 precision)
* @example getPreciseTimestamp(2) 153208746444 (1/100 秒精度)
* @example getPreciseTimestamp(1) 15320874644 (1/10 秒精度)
* @example getPreciseTimestamp(0) 1532087464 (秒精度)
* @example getPreciseTimestamp(-1) 153208746 (10 秒精度)
* @example getPreciseTimestamp(-2) 15320875 (100 秒精度)
*
* @param int $precision
*
* @return float
*/
public function getPreciseTimestamp($precision = 6);
13位时间戳
now()->timestamp . str_limit(now()->micro, 3, '')
16位时间戳
now()->timestamp . now()->micro
也可以添加快捷方法,方便下次用
laravel5.5可以这么写,高版本请参考 carbon 文档
carbon.nesbot.com/docs/#api-macro//返回13位时间戳 Carbon::macro('microTimestamp', function () { return now()->timestamp.str_limit(now()->micro, 3, ''); }); dd(Carbon::microTimestamp()); //13位时间戳