2

Alright when I do the code:

<script type = "text/javascript" >
document.write((new Date).getTime());
</script>

You get the Unix timestamp in miliseconds. I need something that will match this in PHP. I need a variable to be set to that, NOT necessarily printed. The variable needs to be a string OR number without any formatting (decimals, commas, etc).

The numbers don't have to match exactly, but have to be close. I tried doing time()*1000 but it turns it into scientific notation and I couldn't format it out without messing up the string.

Thanks so much for any help

4
  • Demo: jsfiddle.net/CPqRr Commented Feb 12, 2012 at 8:06
  • Is the additional precision (the last three digits) that important? Can you remove them and move on? Commented Feb 12, 2012 at 8:13
  • This will probably help too: stackoverflow.com/a/1063539/451969 Commented Feb 12, 2012 at 8:16
  • You can also use Date.now() to get an equivalent timestamp as well. Not sure about how compatible it is though. Commented Feb 12, 2012 at 9:18

3 Answers 3

1

If you don't need (millisecond) presision, then just divide & Math.floor the javascript's function. So:

time();

and

Math.floor((new Date).getTime()/1000)

should return the same value at the same time.

Sign up to request clarification or add additional context in comments.

Comments

1

What you are looking for is millisecond time in PHP. To accomplish this you need to use a combination of the microtime function and some multiplication.

microtime when passed true as its first parameter will return the current time as the number of seconds since the Unix epoch to the nearest microsecond.

To convert the value into an integer value of the number of milliseconds since the Unix epoch you must multiply this value by 1000 and cast to an integer.

So:

$milliseconds = (int)(microtime(true) * 1000);

4 Comments

Few problems there, microtime() returns the number of milliseconds, not seconds. Also, don't forget the $ sigil.
@alex: microtime returns seconds when passed true as the parameter. The microseconds are given as the value after the decimal. Take a look at the linked manual page; it was a new addition in PHP 5.
@MitMaro I shall read more of the documentation before I open my mouth :)
This code fails on 32-bit builds of PHP as it overflows. suggest use floor(), or from php 6 onwards, (long)().
0

The javascript

getTime() ;

method returns the number of milliseconds since midnight of January 1, 1970 and the specified date.

A php equivalent is

time() * 1000; // not microtime() as I wrongly said earlier.

However they wont match as php does not support millisecond precision it seems.

5 Comments

Thanks for answering, but...getTime() is outputting 1329034097600 and time() is outputting 1329034082. I have both these being generated on the same page close to the same time.
I don't know if microtime() is what the OP is looking for here. Demo: codepad.org/5WJrKXzf
I tried that as well. microtime just came up with:"0.40322000 1329034292" and getTime() gave: 1329034307542
Doesnt have to be too precise, the problem is that time()*1000 puts it in scientific notation.... I guess I'll just add three zeros as a string instead of making it an integer
@user1019588 - See my Codepad link in my above comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.