Bạn đang xem: How to get current time in milliseconds in php?
Continue reading to fulfill the time requirements of your program effectively.
ContentsCoding ExamplesIncrease the Precision in SecondsGet PHP Timestamp MillisecondsUse PHP microtime to lớn Get Unix Timestamp in MicrosecondsFormat the OutputPHP Timing Functions Returning Time in Seconds Only
What Is microtime in PHP?
The PHP microtime is a built-in function that returns the current Unix timestamp in the “msec sec” format. Here, the “sec” represents the seconds, and the “msec” represents the microseconds after being converted into seconds. The given function accepts a single optional boolean argument “as_float.”
So, the result returned by the microtime in PHP can be read as “microseconds_as_seconds seconds.” Also, you can’t say that the microtime() function returns the current time in microseconds. Furthermore, if you pass “true” lớn the PHP microtime function, you’ll get the current time in seconds only.
The following coding examples have been created to lớn show you how to use the microtime function. Plus, you’ll be able khổng lồ see the difference in the outputs based on the argument passed to lớn the given function.
– PHP microtime Function With Zero Arguments
For example, you want to lớn display the current time in the “microseconds_as_seconds seconds.” Here you’ll simply need lớn call the microtime() function without passing any arguments.
Please see the below one-liner code to lớn get current time with microseconds:
|// using the microtime functionecho microtime();// output: 0.28552700 1642373103?>|
– Passing True khổng lồ the PHP microtime Function
For instance, you want lớn get the current time in seconds. You need to lớn use the PHP microtime function by passing “true” as an argument.
Please have a look at this code block that compares the results obtained before and after passing “true” as an argument:
|$msec_sec = microtime();echo “Time as msec sec: $msec_sec ”;// output: Time as msec sec: 0.19812600 1642423688$seconds = microtime(true);echo “Time in seconds: $seconds”;// output: Time in seconds: 1642423688.1981?>|
Increase the Precision in Seconds
Are you looking for a more precise time returned by the microtime(true) function? Well, it would be beneficial to note that the number of digits returned by the microtime(true) function depends on the value of the “precision” directive in the php.ini file. The said directive has its default value set lớn “14.” But if you want lớn have more digits, you can call the ini_set() function to set your desired number of digits.
– Coding Example
For instance, you want to set the precision to 16 digits instead of 14. Therefore, you’ll hotline the ini_set() function by passing the “precision” & “16” as arguments. Later, you’ll hotline the microtime(true) function lớn get the current time in seconds with more precision.
The coding instructions given here will assist you in increasing the precision:
|// setting the precision to 16ini_set(“precision”, 16);// executing the microtime(true) functionecho microtime(true);// output: 1642439657.329814?>|
Get PHP Timestamp Milliseconds
Would you like to have PHP time in milliseconds? Indeed, you can get the time in milliseconds by using the result returned by the PHP microtime function. All that you’ll need to vì chưng is to lớn multiply the output generated by the microtime(true) function by 1000. You shouldn’t doubt the process because it is a basic mathematics formula, and it is a fact that a second is 1000 times bigger than a millisecond.
– Coding Example
Suppose you have a program that requires time in milliseconds. So, you’ll simply execute the PHP microtime function by passing true as an argument. Consequently, you’ll get the current time in seconds, and then you’ll multiply the obtained seconds by 1000 lớn get PHP time in milliseconds.
The code block shared below will be helpful for you in understanding the entire process along with the accuracy of the result:
|// multiply the result of microtime(true) by 1000$milliseconds = microtime(true) * 1000;echo $milliseconds;// output: 1642423688198.1?>|
Use PHP microtime to lớn Get Unix Timestamp in Microseconds
Is it possible to lớn get the current Unix timestamp in microseconds? Undeniably, you can perform the necessary conversion lớn get time in microseconds, but the issue is the large value. As a microsecond is 1/1000000th part of a second, the resulting time value in microseconds will be long enough. And unfortunately, if you are using a 32-bit Operating System, you won’t get the desired time value.
However, if you have a 64-bit Operating System, you can multiply the output of the microtime(true) function by 1000000 to lớn get the current time in microseconds.
– Coding Example
For instance, you have created a program that calls two functions with a timing difference of five seconds. Now, you want khổng lồ see how many microseconds have elapsed between the two function calls. You’ll need to use the microtime(true) function & multiply the output of the same function by 1000000 at the time of calling the first function. Next, you’ll repeat the same process while executing the second function.
In the end, you’ll minus the second recorded time from the first one to lớn get the elapsed time in microseconds as seen in the code fragment below:
|// getting a part of a string$part = substr(“Welcome to Timing Functions”,5);// getting the time in microseconds$first = microtime(true) * 1000000;// setting the sleeping time to lớn five secondssleep(5);// replacing the word ‘Timing’ with ‘PHP’ in the substringstr_replace(“Timing”,”PHP”,$part);// getting the time in microseconds$second = microtime(true) * 1000000;// calculating the time elapsed between the two function calls$difference = $second – $first;// printing a statement with elapsed microsecondsecho “The word ‘Timing’ was replaced after $difference microseconds of getting the substring.”;?>|
You will get the đầu ra that will look similar to this:
|The word ‘Timing’ was replaced after 5015866 microseconds of getting the substring.|
Format the Output
Can you format the PHP microtime result in such a way that the “microseconds as seconds” follow the “seconds” without a dot và a space? Although there isn’t any special function to provide the time in the mentioned format, you can use the preg_replace() function khổng lồ format the output generated by the microtime in PHP.
– Coding Example
Suppose you have a time requirement in your program that expects the same time value as the one produced by the PHP microtime function, but you aren’t allowed to add a dot & a space. You’ll need to use the preg_replace() function by passing “/(0).(d+) (d+)/” pattern và a replacement string “$3$1$2” along with the output đầu ra of the PHP microtime function.
Also, please look at the code shown here to lớn implement the above example:
|// using the microtime() function$msec_sec = microtime();// printing the time in “msec sec” formatecho “$msec_sec ”;// using the preg_replace() functionecho preg_replace(‘/(0).(d+) (d+)/’, ‘$3$1$2’, $msec_sec);?>|
You will get a result similar khổng lồ this after executing the above code:
PHP Timing Functions Returning Time in Seconds Only
Unquestionably, the PHP microtime function gives quite satisfying results. But if you are concerned only about the current time in seconds, then the time() function can also be a good choice. The stated function returns the current time in seconds without considering the microseconds.
You can use it like this: time().
– Coding Example
Imagine that you want to lớn see how many seconds have passed since the Unix Epoch “January 1 1970 00:00:00 GMT.” Moreover, you want to compare the results of the microtime(true) and time() functions. So, there wouldn’t be a better idea than calling both of the functions at the same time to observe the difference in the obtained results.
You can copy and paste the given code snippet lớn get your current time in seconds by using the microtime(true) và time() functions:
|// using the time functionecho “Result of the time() function:” . Time();// using the microtime functionecho “Result of the microtime(true) function:” . Microtime(true);?>|
Until you try out the above code, you can notice the difference in the outputs here:
|Result of the time() function:1642432244Result of the microtime(true) function:1642432244.2976|
Summarizing the article, you have mastered the use of the PHP microtime function along with using the đầu ra of the same to lớn generate different results. Now, it’s time to go through all the significant details to lớn ensure that you don’t over up mixing any concepts:The PHP microtime function returns the current Unix timestamp in “microseconds_as_seconds seconds” format.The microtime(true) can be used lớn get the current Unix timestamp in seconds.You can increase the number of digits returned by the microtime(true) by increasing the value of the precision directive.You can multiply the output of the microtime(true) by 1000 to lớn get the current time in milliseconds.You can get the current time in microseconds by multiplying the result of the microtime(true) by 1000000.