'What is the function of the (new Date()).getTime() in PHP?

I was looking at some javascript code:

<script language="javascript" type="text/javascript">
var time = (new Date()).getTime();
console.log(time);
</script>

What is the function of (new Date()).getTime() in php?

because i want try to make it into php language , but with microtime() still different output with javascript. what should i do?



Solution 1:[1]

In JavaScript (new Date()).getTime() returns the number of milliseconds since 1970/01/01.

If you want to make a porting to PHP you can use time()*1000.

time(void) returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

In PHP you can multiply for 1000 the result of time() to achive the same behaviour of (new Date()).getTime() in JavaScript.

In your code you can write something like:

<?php
...
echo time()*1000;
...
?>

Note that PHP is executed server side and from there you can't access to the browser console.
If you want to literally port the script you've provided, you have to use a bit of JavaScript to print the result to the console.

You can take a look here:

Solution 2:[2]

Time in php

$time_stamp = time();
echo $time_stamp;

Or

echo microtime(true)*1000;

Solution 3:[3]

Try with microtime().

<html>
<script language="javascript" type="text/javascript">
    var time = (new Date()).getTime();
    console.log(time);
</script>
<?php
    $str = microtime();
    $pecah = explode(" ", $str);
    print $pecah[1];
?>
</html>

Solution 4:[4]

Try this: Strtotime('now');

Solution 5:[5]

In JavaScript, (new Date()).getTime() prints out time from unix epoch in milliseconds. To achieve the same in PHP, you can simply do

<?php echo (int)(microtime(true)*1000); ?>

To get the equivalent value.

Solution 6:[6]

There are several ways. Use php function:

time();

Example:

$t=time();  
echo $t;

You also can get time using:
date("h:i:sa");

Solution 7:[7]

For higher precision, you may try this :

<?php echo round(microtime(true) * 1000); ?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Popnoodles
Solution 2 ashkufaraz
Solution 3 Dan Lowe
Solution 4 Murat Cem YALIN
Solution 5 jpaljasma
Solution 6 KittMedia
Solution 7 John Traviss