Skip to content

Instantly share code, notes, and snippets.

@willhalling
Created April 18, 2013 20:08
Show Gist options
  • Save willhalling/5415832 to your computer and use it in GitHub Desktop.
Save willhalling/5415832 to your computer and use it in GitHub Desktop.
Latest Tweets ordered by date (jQuery)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Latest Tweets Ordered by Date</title>
<meta name="robots" content="noindex, nofollow" />
</head>
<body>
<select id="test">
<option selected="selected" value="dateDesc">Date Descending</option>
<option value="dateAsc">Date Ascending</option>
</select>
<?php
require('twitter.class.php');
$tweets->get_tweets('YOUR_TWITTER_USERNAME');
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function sortDateDescending(a, b) {
var date1 = $(a).find(".date").text();
date1 = date1.split('.');
date1 = new Date(date1[2], date1[1] - 1, date1[0]);
var date2 = $(b).find(".date").text();
date2 = date2.split('.');
date2 = new Date(date2[2], date2[1] - 1, date2[0]);
return date1 < date2 ? 1 : -1;
}
function sortDateAscending(a, b) {
var date1 = $(a).find(".date").text();
date1 = date1.split('.');
date1 = new Date(date1[2], date1[1] - 1, date1[0]);
var date2 = $(b).find(".date").text();
date2 = date2.split('.');
date2 = new Date(date2[2], date2[1] - 1, date2[0]);
return date1 > date2 ? 1 : -1;
}
$(document).ready(function () {
var desc = false;
document.getElementById("test").onchange = function () {
if (document.getElementById("test").value == "dateAsc") {
$('ul > li').sort(sortDateAscending).appendTo('ul');
} else if (document.getElementById("test").value == "dateDesc") {
$('ul > li').sort(sortDateDescending).appendTo('ul');
}
return false;
};
//Sorts descending based on value of date as default.
$('ul > li').sort(sortDateDescending).appendTo('ul');
});
</script>
</body>
</html>
<?php
class Twitter {
function get_tweets($userid){
if(simplexml_load_file('https://api.twitter.com/1/statuses/user_timeline/'.$userid.'.xml?count=20')) {
$xml = simplexml_load_file('https://api.twitter.com/1/statuses/user_timeline/'.$userid.'.xml?count=20');
$tweets = $xml->xpath("/statuses/status");
echo '<ul id="tweets">';
foreach($tweets as $tweet) {
$text = $tweet->text;
$date = $tweet->created_at;
$date = date("d.m.Y", strtotime($date));
echo '<li>' . $text . ' <span class="date">' . $date . '</span></li>';
}
echo '</ul>';
}
else echo 'error';
}
}
$tweets = new Twitter;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment