JQuery.get() function returning undefined

jweav1
jweav1 used Ask the Experts™
on
I am pulling data from twitter and need to compare the date a tweet was originated to the current date.

Twitter returns a created_at item in its JSON response which is formatted as
"Fri, 02 Jul 2010 14:03:33 +0000"  
So to make the date comparison I am pulling each date and sending it to a strtotime php function which converts it to a timestamp which can be easily compared to a timestamp of the current date.

I am sending each date individually from the JSON array because it would be to big and cumbersome to send them all attached to the URL.

This all works fine and the times are being converted.

The problem is: my convertDates function is supposed to return the converted tweetDate and load it into the converetedDates array, but the function keeps returning undefined.

I've logged the convertDates function output right before it returns and it is showing the correct timestamp output, but something is happening when it returns.

The code is attached.

Thanks in advance.




$(document).ready(function () {	
							
	 var date;
	 var convertedDates = [];
	 var theTweets;

	function convertDates(dates){ 
		$.get('convert_dates.php', {
			dates: dates
			}, function (response) {
				tweetDate = response.date;
				
				return tweetDate;
				
			}, 'json');
		
	};	
							
	jQuery.ajax({
      url: 'http://search.twitter.com/search.json?rpp=50q=happy',
	 dataType: 'jsonp',
	success: function (data) {
		theTweets = data.results;
					 
	        for (i=0; i<theTweets.length; i++){
	 
		     date = theTweets[i].created_at;
					 
		     convertedDates[i] = convertDates(date);
						
		}
					 
	       console.log(convertedDates);
					
	    }
	
	});
	 	
});



just in case here is the php for convert_dates.php:

<?php



$dates = $_GET['dates'];

function convertDates($dates){

$length = count($dates);

$newDates = array();

for ($x=0; $x<$length; $x++) {
	
	$newDates[date] = strtotime($dates[$x]);
	
}

return $newDates;

}

$result = convertDates($dates);

echo json_encode($result);

?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think the problem is that your callback function isn't being called by anything other than the $.get command. As a result, returning the tweet date there isn't assigning it to anything. Try something more like:

var tDate may need to be moved into a more global context in order for this to work. I'm not 100% sure.
function convertDates(dates){ 
		
                var tDate = null;
                $.get('convert_dates.php', {
			dates: dates
			}, function (response) {
				tweetDate = response.date;			
				tDate = tweetDate;
				
			}, 'json');
		
                return tDate;
	};

Open in new window

In retrospect, you don't need the tDate variable at all, just define var tweetDate outside of your callback first.
Multitechnician
Top Expert 2014
Commented:
Work for me :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript">

	var convertedDates = [];

	function convertDates(dates) {
		var tweetDate = 0;
		$.get('convert_dates.php', {dates: dates}, function(response) {
			convertedDates[convertedDates.length] = response.date;
		}, 'json');
	}

	$(document).ready(function () {			
		jQuery.ajax({
			url: 'http://search.twitter.com/search.json?rpp=100&q=&since_id=18826944773&geocode=38.627522%2C-90.19841%2C15mi',
			dataType: 'jsonp',
			success: function (data) {
				var theTweets = data.results;
				for (i=0; i<theTweets.length; i++) {				
					convertDates( theTweets[i].created_at );
				}		
				console.log(convertedDates);
			}
		});
	});

</script>
</head>
<body>
</body>
</html>

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
Hello jweav1,

No news from you, do you need more help ?

Kind Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial