Anisha Virat
Anisha Virat

Reputation: 259

Get date+time timestamp

I need a timestamp code similar to this JavaScript code:

new Date().getTime()

I tried this PHP code:

$date = new DateTime();
$ts = $date->getTimestamp();

which returns 1376399143 but the JavaScript code returns 1376399143263, I think my PHP code generates a timestamp for only the date. How can I get the timestamp for the time portion as well?

Upvotes: 0

Views: 77

Answers (4)

juanra
juanra

Reputation: 1642

The php code generates the timestamp for date and time, but it returns the number of seconds from 1-1-1970. Javascript returns the value in miliseconds.

You could use microtime(true)*1000; for a similar result that Javascript.

Upvotes: 0

Ripa Saha
Ripa Saha

Reputation: 2540

use the code below

$time = mktime(date("H"),date("i"),date("s"),date("n"),date("j"),date("Y"));
echo $time;

for details information on mktime , check this link

Upvotes: 1

Bora
Bora

Reputation: 10717

You should use microtime

$seconds = microtime(true); // false = int, true = float 
echo round(($seconds * 1000));

Upvotes: 0

tobspr
tobspr

Reputation: 8376

You could use microtime, but if you do not need the additional precision (which will get lost because of networking), just divide the javascript time by 1000.

Upvotes: 0

Related Questions