在php中为时间添加微秒 [英] add microseconds to a time in php

查看:39
本文介绍了在php中为时间添加微秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来以微秒为单位获取当前时间的代码,

Here is the code i used to get the current time with microseconds,

 $time=date("Y-m-d\TH:i:s") . substr((string)microtime(), 1, 8);

我需要通过添加一些微秒值来更改 $time 中的值.是否可以将微秒添加到时间?实际上我需要创建一个模拟器,这里我需要检查一秒间隔内发生了多少次执行,并找出执行所需的微秒数.

I need to change the value in $time by adding some values in microsecond.Is possible to add microseconds to the time?? actually i need is need to create a simulator,Here i need to check how many executions are taking place in the interval of one second,and find how many microseconds needed for the execution.

推荐答案

您可以像这样为旧版本构建回退功能.(只对当前时间有效)

You can build a fallback function for older versions like this. (It only works for the current time)

function udate($format, $utimestamp = null){
                if(is_null($utimestamp)){
                    $utimestamp = microtime(true);
                    $timestamp = floor($utimestamp);
                    $milliseconds = round(($utimestamp - $timestamp) * 1000000);
                    return date(str_replace("u",$milliseconds,$format), $timestamp);
                }else
                    return date($format, $utimestamp);
            }

echo udate('Y-m-d H:i:s.u');

这篇关于在php中为时间添加微秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆