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?

1
  • 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? 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. Commented Oct 23, 2017 at 19:58

4 Answers 4

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,'.','');
Sign up to request clarification or add additional context in comments.

Comments

6
<?php
$x = time(); // seconds
$x = microtime(); // milliseconds
?>

Check out time() and microtime().

Comments

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).

Comments

0

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

<?php 
    $time = time();
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.