5

Suppose I'm running the following JavaScript getTime() function

<script language="JavaScript">
var $x = Math.round((new Date()).getTime()/1000);
</script>

What would the equivalent code in PHP look like?

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
Mark Kennedy
  • 1,751
  • 6
  • 30
  • 53
  • 3
    JavaScript time is in milliseconds and PHP time is in seconds. This answer needs to be reopened to answer this question correctly, as none of the answers below is correct, and it is not answered for JavaScript in [How to get the current date and time in PHP?](https://stackoverflow.com/questions/470617/how-to-get-the-current-date-and-time-in-php) The correct answer to the question in case this doesn't get reopened is: `( (string)time() ) . ( (string)intval(microtime()*1000) )` and yes all the parentheses are necessary, otherwise it drops a digit. – Jim Bergman Oct 23 '17 at 19:58

4 Answers4

10

Try something like this:

echo number_format(microtime(true)*1000,0,'.','');

or because you are dividing by 1000, most probably you need this:

echo number_format(microtime(true),0,'.','');
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
6
<?php
$x = time(); // seconds
$x = microtime(); // milliseconds
?>

Check out time() and microtime().

Mr. B.
  • 8,041
  • 14
  • 67
  • 117
1

Unix time in php

$time_stamp = time();
echo $time_stamp;

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

Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
0

You'd use the php function time(). It creates the timestamp for you.

<?php 
    $time = time();
?>
Darren
  • 13,050
  • 4
  • 41
  • 79