添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
英俊的桔子  ·  python ...·  10 月前    · 
坚强的手链  ·  使用 FFmpeg ...·  1 年前    · 
近视的手套  ·  2018-10-28 ...·  1 年前    · 

ps: 写于2018年

由于被分配任务说要做一个调用链追踪(实际上就是用的zipkin实现的),在实现过程中踩了一个坑。
问题需求,获得当前微秒级别时间戳。issue地址:https://github.com/whitemerry/phpkin/issues/18

别人的解决方案

//直接上代码
    function zipkin_timestamp()
        return intval(microtime(true) * 1000 * 1000);

出现的问题

线上linux系统没有问题,跑出来结果是正确的,
windows系统错误
microtime();
(PHP 4, PHP 5, PHP 7)
microtime — 返回当前 Unix 时间戳和微秒数
mixed microtime ([ bool $get_as_float ] )
microtime() 当前 Unix 时间戳以及微秒数。本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。
如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
如果给出了 get_as_float 参数并且其值等价于 TRUE,microtime() 将返回一个浮点数。

最后解决方案

//直接上代码
//生成时间戳(存储数据时应该用一个bigint)
    function zipkin_timestamp()
        $str = microtime();
        $fstr =  substr($str,11,10).substr($str,2,6);
        return $fstr;
//验证函数 
    function is_zipkin_timestamp($timestamp)
        return ctype_digit((string) $timestamp) && strlen($timestamp) === 16;
windows下的问题就fixed了
                    php获取当前时间戳(16位长度,微秒单位)ps: 写于2018年由于被分配任务说要做一个调用链追踪(实际上就是用的zipkin实现的),在实现过程中踩了一个坑。问题需求,获得当前微秒级别时间戳。issue地址:https://github.com/whitemerry/phpkin/issues/18别人的解决方案//直接上代码    function zipkin_timesta...
				
2023年6月15日16:13:29 date()函数默认只能获取到秒级的时间戳,无法直接获取微秒级的时间 建议使用 DateTime or DateTimeImmutable DateTime 此类与 DateTimeImmutable 行为相同。区别就是调用有修改行为的方法(比如 DateTime::modify())时,会修改对象本身。 在 DateTime 类的对象上调用方法会改变...
select account_id,is_cancel,cancel_date from ams_account where account_id='讠午牙隹王令'; select FROM_UNIXTIME("1604158088748"); select FROM_UNIXTIME('1604158088748'/1000,'%Y-%m-%d %H:%i:%S') -- 先转成 10