Convert UNIX timestamp to milliseconds
Asked Answered
D

3

6

How can I use PHP to get a UNIX timestamp like what I get from the JS method .getTime()? I seem to be having trouble since .getTime() returns milliseconds. I know I have to convert the timestamps first for JS to read it, but how can I do this?

Edit:

Agreed with the multiply by 1000, but why do I get this?:

timestamp: 1305593400
timestamp * 1000: 1.3055934E+12

timestamp: 1305612420
timestamp * 1000: 1.30561242E+12

timestamp: 1305635400
timestamp * 1000: 1.3056354E+12

timestamp: 1304901960
timestamp * 1000: 1.30490196E+12

timestamp: 1304944620
timestamp * 1000: 1.30494462E+12
Dukas answered 21/6, 2011 at 18:23 Comment(0)
B
19

UNIX timestamps are in seconds. Multiply by 1000.

Burlburlap answered 21/6, 2011 at 18:24 Comment(3)
The new numbers are too large to fit into an int. But they're still the appropriate number.Burlburlap
But my JS code cannot read it properly, but with a proper JS getTime() timestamp works just fine but not this. So this doesn't entirely answer my question.Dukas
Works fine here. js> Date(1.3055934E+12) Tue Jun 21 2011 14:35:12 GMT-0400 (EDT)Burlburlap
S
5

If you really need proper presentation -- use number_format().

$timestamp = 1305593400;
$ms = $timestamp * 1000;
echo number_format($ms, 0, '.', '');

Result: 1305593400000

Supersession answered 21/6, 2011 at 18:39 Comment(0)
C
0

I use it

$unix_date = (time("Ymd", strtotime($r->date)) *1000);

Cavuoto answered 18/6, 2018 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.