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

kmoney12
  • 4,413
  • 5
  • 37
  • 59

3 Answers3

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);
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
MitMaro
  • 5,607
  • 6
  • 28
  • 52
  • Few problems there, `microtime()` returns the number of milliseconds, not seconds. Also, don't forget the `$` sigil. – alex Feb 12 '12 at 08:26
  • 1
    @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 Feb 12 '12 at 08:28
  • 2
    @MitMaro I shall read more of the documentation before I open my mouth :) – alex Feb 12 '12 at 08:32
  • This code fails on 32-bit builds of PHP as it overflows. suggest use floor(), or from php 6 onwards, (long)(). – Sam Feb 28 '13 at 13:41
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.

Rok Kralj
  • 46,826
  • 10
  • 71
  • 80
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.

mbh
  • 3,302
  • 2
  • 22
  • 24
  • 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. – kmoney12 Feb 12 '12 at 08:08
  • I don't know if `microtime()` is what the OP is looking for here. Demo: http://codepad.org/5WJrKXzf – Jared Farrish Feb 12 '12 at 08:10
  • I tried that as well. microtime just came up with:"0.40322000 1329034292" and getTime() gave: 1329034307542 – kmoney12 Feb 12 '12 at 08:13
  • 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 – kmoney12 Feb 12 '12 at 08:17
  • @user1019588 - See my Codepad link in my above comment. – Jared Farrish Feb 12 '12 at 08:21