I want to get current Timestamp with millisecond. but date()
returns Timestamp just with seconds.
Asked
Active
Viewed 3,434 times
2

Cœur
- 37,241
- 25
- 195
- 267

Majid Sadr
- 911
- 7
- 18
-
[time()](http://php.net/manual/en/function.time.php) – Bonatti May 25 '16 at 11:16
-
1@Bonatti This question is about millisecond but you duplicated my question with a question about date/time (just second) – Majid Sadr May 25 '16 at 11:18
-
Read the answers, specially about time() functions. This question shows no effort to Google what time functions exists in PHP, so I just marked one that has the issue. – Bonatti May 25 '16 at 11:20
2 Answers
2
You can use microtime (which is better solution because you will get exact value).

Gynteniuxas
- 7,035
- 18
- 38
- 54
-
-
1And in any case, multiplying won't work because you can't add precision to a number. `microtime()` is the correct solution. – Chris May 25 '16 at 11:10
-
-
1I have some records in my database that have timestamp, but the question is how to fetch with milliseconds. or save timestamp with milliseconds. – Majid Sadr May 25 '16 at 11:12
-
1That's a terrible suggestion. You will still only have seconds only represented by 1000's of miliseconds. – apriede May 25 '16 at 11:12
-
@MajidSadr So, to be clear, by timestamp you don't mean a Unix timestamp? – Chris May 25 '16 at 11:14
-
1
-
2@MajidSadr so why exactly microtime() is not fine? It returns time in microseconds? – Gynteniuxas May 25 '16 at 11:20
-
1EdvinTenovim Your last edit solved my problem. But Thank you @Chris – Majid Sadr May 25 '16 at 11:22
-
2@MajidSadr His edits never actually changed the semantics of the answer though. The original said to use `microtime()` too. In any case, I'm glad you solved it. You should be able to accept the answer if it worked for you. – Chris May 25 '16 at 11:24
-
I moved the first part up because it is more appropriate than the second one (multiplication). – Gynteniuxas May 25 '16 at 11:25
-
3@EdvinTenovim, I would sugest you just remove the second part since it is simply mind numbingly bad and wrong. It's like suggesting using MS Paint zoom to substitute for an electron microscope – apriede May 25 '16 at 11:27
-
1
You may try below code from source
$now = DateTime::createFromFormat('U.u', microtime(true));
echo $now->format("m-d-Y H:i:s.u");

Community
- 1
- 1

Ankit Doshi
- 1,164
- 3
- 21
- 43
-
1It returns negative results some times: `05-25-2016 11:31:25.-339967296` – Majid Sadr May 25 '16 at 11:32
-