❮ PHP Date/Time Reference
Example
Return the current Unix timestamp with microseconds:
Try it Yourself »
Definition and Usage
The microtime[] function returns the current Unix timestamp with microseconds.
Syntax
Parameter Values
return_float | Optional. When set to TRUE it specifies that the function should return a float, instead of a string. Default is FALSE |
Technical Details
Returns the string "microsec sec" by default, where sec is the number of seconds since the Unix Epoch [0:00:00 January 1, 1970 GMT], and microsec is the microseconds part. If the return_float parameter is set to TRUE, it returns a float representing the current time in seconds since the Unix epoch accurate to the nearest microsecond |
4+ |
PHP 5.0: Added the return_float parameter |
❮ PHP Date/Time Reference
I've looked all around, and there seem to be a lot of hacks, but no simple, "good" ways to do this. I want to convert a Python datetime
object into microtime like time.time[]
returns [seconds.microseconds].
What's the best way to do this? Using mktime[]
strips off the microseconds altogether, you could conceivably
build up a timedelta
, but that doesn't seem right. You could also use a float[strftime["%s.%f"]]
[accounting for rounding seconds properly], but that seems like a super-hack.
What's the "right" way to do this?
agf
163k40 gold badges278 silver badges232 bronze badges
asked Aug 30, 2011 at 2:58
0
time.mktime[dt.timetuple[]] + dt.microsecond / 1000000.0
works if you don't want to use strftime
and float
.
It returns the same thing as time.time[]
with dt = datetime.datetime.now[]
.
answered Aug 30, 2011 at 3:20
agfagf
163k40 gold badges278 silver badges232 bronze badges
6
def microtime[dt]:
unixtime = dt - datetime.datetime[1970, 1, 1]
return unixtime.days*24*60*60 + unixtime.seconds + unixtime.microseconds/1000000.0
answered Aug 30, 2011 at 3:10
Mark RansomMark Ransom
290k40 gold badges383 silver badges606 bronze badges
6
import time;
microtime=int[time.time[]*1000];
print microtime;
answered Sep 27, 2018 at 12:20
1
Not the answer you're looking for? Browse other questions tagged python datetime epoch or ask your own question.
[PHP 4, PHP 5, PHP 7, PHP 8]
microtime — Return current Unix timestamp with microseconds
Description
microtime[bool $as_float
= false
]: string|float
For performance measurements, using hrtime[] is recommended.
Parameters
as_float
If used and set to true
, microtime[] will return a float instead of a string, as described in the return values section below.
Return Values
By default, microtime[] returns a string in the form "msec sec", where sec
is the number of seconds since the Unix epoch [0:00:00 January 1,1970 GMT], and msec
measures microseconds that have elapsed since sec
and is also expressed in seconds as a decimal fraction.
If as_float
is set to true
, then microtime[]
returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.
Examples
Example #1 Timing script execution
Example #2 microtime[] and REQUEST_TIME_FLOAT
See Also
- time[] - Return current Unix timestamp
- hrtime[] - Get the system's high resolution time