Jump to content

Help with casting and big numbers


yuckysocks

Recommended Posts

Hi all. I'm trying to get data into a form that works with flot, the jQuery graphing library. I need to get UTC timestamps from Twitter into a javascript timestamp (=Unix timestamp * 1000). My code currently looks like this:

		foreach($decoded['results'] as $currentResult) {
	     if($currentResult['from_user'] == $user) {
	        $strippedTexts = $currentResult['created_at'];
			$bignumber = strtotime($strippedTexts);
			$biggerstring = bcmul($bignumber, 1000);
			$dates []=  settype($biggerstring, "int");
	     }
	}

 

This creates a dates[] array that contains the value "1", once. Clearly not what I want.

 

I've tried multiplying by 1000 (resulted in a large number barf: -2147483648), multiplying by 1000.0 (to convert to a float, maybe it would take a bigger number). I've also tried concatenating "000" and using setype($var, "float") and that won't work either. Any idea how I can get the incoming array of UTC time stamps into "milliseconds since 1970" (which is what the flot library needs, as an integer, not a string)?

 

If it helps, here's the entire code, soup to nuts:

<?php
$url = "http://search.twitter.com/search.json?q=%23runlogger";
$contents = file_get_contents($url); 
$decoded = (json_decode($contents, true));
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.flot.js"></script>
</head>
<body>

<form name="input" action="skeleton.php" method="get"> Twitter Username:
<input type="text" name="user" />
<input type="submit" value="Go Get 'em, Tiger" />
</form>

<?php
    if (isset($_GET['user'])) {
    	$miles = array();
	$dates = array();
	$pairs = array();
	$user = $_GET["user"];

	foreach($decoded['results'] as $currentResult) {
	     if($currentResult['from_user'] == $user) {
	         $strippedTexts = explode(" ", $currentResult['text']);
	         $miles []= $strippedTexts[0];
	     }
	}

	foreach($decoded['results'] as $currentResult) {
	     if($currentResult['from_user'] == $user) {
	        $strippedTexts = $currentResult['created_at'];
			$bignumber = strtotime($strippedTexts);
			$biggerstring = bcmul($bignumber, 1000);
			$dates []=  settype($biggerstring, "int");
	     }
	}

	foreach($miles as $key=>$mile) {
		$pairs[$dates[$key]] = $mile;

	}
	ksort($pairs);

	foreach($pairs as $d=>$m) {
		$orderedpairs .= "[$d, $m], ";
	}
	echo "
	<div id='placeholder' style='width:600px;height:300px'></div>
	<script language='javascript' type='text/javascript'>  
	$(function () {  
	    var data = [[$orderedpairs]];  
	    var plotarea = $('#placeholder');   
		var options = {
			legend: {
				show: true
			}
			xaxis: {
				mode: time
			}
		}
	    $.plot( plotarea , data , options );  
	});  
	</script>
	$orderedpairs
	";
        }

    else {

      echo "Go ahead and search for a name! Try 'Tester2314' to see what an example could look like";

         }

    ?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.