How to get the current unix timestamp in php using microtime?
(PHP 4, PHP 5, PHP 7, PHP 8) Show microtime — Return current Unix timestamp with microseconds Descriptionmicrotime(bool For performance measurements, using hrtime() is recommended. Parametersas_float If used and set to Return Values By default, microtime() returns a string in the form "msec sec", where If ExamplesExample #1 Timing script execution
"Did nothing in $time seconds\n"; Example #2 microtime() and
"Did nothing in $time seconds\n"; See Also
Short answer:64 bits platforms only!
[ If you are running 64 bits PHP then the constant Long answer:If you want an equilvalent function of The size of an integer in PHP can be 32 or 64 bits depending on platform. From http://php.net/manual/en/language.types.integer.php
If you have 64 bits integers then you may use the following function:
The second number is the seconds (integer) while the first one is the decimal part. The above function
then adds the decimal part multiplied by
Note that both Finally, that function is slightly more precise than
that with a ratio of 1:10 (approx.) returns 1 more millisecond than the correct result. This is due to the limited precision of the float type ( Even if it's beyond the scope of the question it's worth mentioning that if your platform supports 64 bits integers then you can also get the current time in microseconds without incurring in overflow. If fact That's the same value you get with
In other words, a signed 64 bits integer have room to store a timespan of over 200,000 years measured in microseconds. You may have then
How can get current date and time in milliseconds PHP?This uses the same timestamp: $t = gettimeofday(); echo date('Y-m-d H:i:s.',$t['sec']) . $ t['usec']; – Savvas Radevic. Oct 2, 2017 at 10:04.. microseconds should be zero padded: $timeofday=gettimeofday(); echo sprintf("%s.%06d", date('Y-m-d H:i:s', $timeofday['sec']), $timeofday['usec']); – mabi. May 20, 2020 at 5:26.. What is Unix timestamp in PHP?Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note: Unix timestamps do not contain any information with regards to any local timezone.
What is a Microtime?Definition of microtime
: a very short interval of time (as 0.01 millionth of a second) microtime photography.
How can I timestamp in PHP?The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
|