5

Possible Duplicate:
php get microtime from date string

For a specific purpose, I need to get the 13-digit timestamp for a date/time, but I couldn't find a solution for this.

Like mktime in PHP, we can get a 10-digit timestamp

echo mktime($hour, $minute, $second, $month, $day, $year);

This outputs something like 1346689283.

So what about the 13-digit timestamp? Is PHP itself capable of generating such timestamps?

Community
  • 1
  • 1
user1643156
  • 4,407
  • 10
  • 36
  • 59

2 Answers2

12

You are looking for microtime()

http://php.net/microtime

If you want to create microtime from a specific date, just add 000. Because in the end, the microtime is only 1 second.

Rene Pot
  • 24,681
  • 7
  • 68
  • 92
  • yes, but I think he wants something that can create microtime from arbitrary dates, like mktime or strtotime would allow for unix timestamps – Gordon Sep 03 '12 at 08:28
  • 1
    @gordon well added that to the answer – Rene Pot Sep 03 '12 at 08:29
  • yep, Gordon's right, microtime() generates CURRENT timestamp with microseconds, and it will not allow me to specify year/month/day/hour/... Example: I need to get the 13-digit timestamp for 1998-02-13 17:44 – user1643156 Sep 03 '12 at 08:31
  • But there are no microseconds in "1998-02-13 17:44"? You might as well add '000' like Rene Pot said. – Terry Seidler Sep 03 '12 at 08:44
  • @user1643156 the microseconds are 1 second. Do you need it so specific? You can add any 3 random numbers like you want. It is not gonna make a big difference anyways. – Rene Pot Sep 03 '12 at 08:58
  • @Rene Pot, by adding 000 seems not working (maybe just in the program I'm using), it results a 1970 error. but 001 - 999 they all work. Anyway, why didn't I just try adding 3 numbers before posting... :) thanks! – user1643156 Sep 03 '12 at 09:18
8

Make milliseconds:

$milliseconds = round(microtime(true) * 1000);
echo date("Y-m-d",($milliseconds/1000));
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107