views:

612

answers:

3

Hi there,

I'm using the flot graphing library for jQuery, and it uses javascript time for any time series (to remind, that's milliseconds since Jan 1970. Unix time is seconds).

My current code looks like this:

foreach($decoded['results'] as $currentResult) {
      if($currentResult['from_user'] == $user) {
          $strippedTexts = $currentResult['created_at'];
          $dates []= strtotime($strippedTexts);
      }
 }

This gives me an array of Unix time stamps. I want to prep the data for JavaScript in the loop, but when I try

$dates []= 1000*strtotime($strippedTexts);

the number is too big and it spits out "[-2147483648]". Do I need to change the "type" of variable allowed to be held in the array to bignum or something?

Thanks!

+2  A: 

You can try using the BCMath Arbitrary Precision functions if you have them available:

$dates[] = bcmul("1000", strtotime($strippedTexts));

Or just, you know, append three zeros on the end.

$dates[] = strtotime($strippedTexts).'000';

In both cases you'll end up with the value being stored as a string, but that shouldn't matter for your usage.

Chad Birch
I tried: $strippedTexts = $currentResult['created_at']; $bigstring = strtotime($strippedTexts).'000'; $dates []= settype($bigstring, "float");And it produces an array of all "1"s. Unfortunately, the javascript library I'm using needs the results as a number, not a string. Any other thoughts?
Alex Mcp
How do you get the variables to the javascript? Javascript can't read PHP variables, so you must be passing it somehow. It shouldn't care whether it's a string or a number at that point. If you can show how you're actually using the $dates array I can have a look at that.
Chad Birch
+3  A: 

Try this:

$dates []= 1000.0*strtotime($strippedTexts);

That will turn it into float, which in php can store a bigger number than an int.

Paul Tomblin
Does each element of an array have it's own variable type then?
Alex Mcp
PHP is loosely typed. I think you can mix any types in an array.
Paul Tomblin
I'm finally back to my test machine; this still results in the barfing of a large negative number and then the array halts. Gross, barf!
Alex Mcp
@Alex, I think you need to try Chad's idea then. And change the accepted answer.
Paul Tomblin
A: 

No solution is required, because there is no problem: have JavaScript do the multiplication.

Robert L