2

I want to get current Timestamp with millisecond. but date() returns Timestamp just with seconds.

Cœur
  • 34,719
  • 24
  • 185
  • 251
Majid Sadr
  • 673
  • 5
  • 16
  • [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 Answers2

2

You can use microtime (which is better solution because you will get exact value).

Gytis TG
  • 6,945
  • 17
  • 35
  • 52
  • Not divide by .It should be multiply – Web Artisan May 25 '16 at 11:09
  • 1
    And 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
  • Oops, yes, multiply it, not divide. Sorry about that. – Gytis TG May 25 '16 at 11:10
  • 1
    I 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
  • 1
    That'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
    @Chris I mean Unix Timestamp – Majid Sadr May 25 '16 at 11:20
  • 2
    @MajidSadr so why exactly microtime() is not fine? It returns time in microseconds? – Gytis TG May 25 '16 at 11:20
  • 1
    EdvinTenovim 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). – Gytis TG 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
  • @apriede, yes, I agree about that. I should remove that part. – Gytis TG May 25 '16 at 11:29
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,126
  • 3
  • 17
  • 40