How do I adjust twitter api timezone? - php

I want to adjust the timezone of the twitter api to my local timezone. Right now it displays:
Thu Apr 03 14:34:29 +0000 2014
However, here it is
16:34:29 (GMT+2).
Code:
if(isset($_POST['keyword']))
{
$url = 'https://api.twitter.com/1.1/search/tweets.json?q='.$_POST['keyword']
.'&lang=nl&result_type=recent&count=25';
$tweets = $twitter->get($url);
foreach($tweets as $tweet)
{
foreach ($tweet as $t)
{
echo '<div class = "twitterText">';
echo '<img src = "'.$t->user->profile_image_url.'"/>'.'<br />'
.$t->user->name.'<br />'
.$t->created_at.'<br /><br />'
.'<p class = "description">'.$t->text.'</p>'.'<br />';
echo '</div>';
}
}
}

I figured it out myself, working code is:
// ...
foreach ($tweet as $t) {
$date = new DateTime($t->created_at);
$date->setTimezone(new DateTimeZone('Europe/Amsterdam'));
$formatted_date = $date->format('H:i, M d');
echo $formatted_date;
}

Using the Tweet timestamp_ms JSON attribute is so much easier than using created_at.

Related

How can I get data from RSS by date in PHP?

I am working on a small script where I wanna take only my website's latest post which is posted yesterday mean I wanna get all yesterday's links and titles.
I tried with my script but I am getting all URLs I am not sure how can I fix it.
Can anyone help me solve this problem?
I was wondering if I can use 'where' attribute like we usually use in SQL. I want only 1 days posts to be scraped.
<?php
header('Content-Type: application/json');
$url = "https://www.lifegoals.co.in/feed/";
$i=0;
$invalidurl = false;
if(#simplexml_load_file($url)){
$feeds = simplexml_load_file($url);
}else{
$invalidurl = true;
echo "<h2>Invalid RSS feed URL.</h2>";
}
if(!empty($feeds)){
//$site = $feeds->channel->title;
//$sitelink = $feeds->channel->link;
//echo "<h1>".$site."</h1>";
foreach ($feeds->channel->item as $item) {
$title = $item->title;
$link = $item->link;
//$description = $item->description;
$postDate = $item->pubDate;
$pubDate = date('D, d M Y',strtotime($postDate));
$currDate = date('D, d M Y');
if($i>=10) break;
if($pubDate=$currDate){
$rss = "<item>
<title>$title</title>
<link>$link</link>
</item>";
echo $rss;
$i++;
}
}
}
?>
i want only 1 days posts there are 4 days posts
I'd add some debugging to this to ensure that you're getting what you think you want. Try the following in your foreach loop:
print_r([
$postDate,
$pubDate,
$currDate,
($pubDate == $currDate),
]);
if($pubDate==$currDate){
$rss = "<item>
<title>$title</title>
<link>$link</link>
</item>";
echo $rss;
$i++;
}
The == was missing thanks.

Getting span contents with “Simple HTML DOM”

I'm trying to scrape Twitter tweets from a user page using “Simple HTML DOM”.
I can get the tweets but not their timestamp.
The HTML seems to be like this:
<p class="ProfileTweet-text js-tweet-text u-dir" lang="en" dir="ltr" data-aria-label-part="0">Tweet content<a href="/hashtag/TweetContent?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>TweetContent</b></a> <a href="http://t.co/JFredfvgYs" class="twitter-timeline-link u-hidden" data-pre-embedded="true" dir="ltr" >pic.twitter.com/JFredfvgYs</a></p>
The UNIX timestamp is in this:
<span class="js-short-timestamp "
data-aria-label-part="last"
data-time="1411584273"
data-long-form="true" >
Sep 24
</span>
So I'm doing:
<?php
include 'simple_html_dom.php';
$html = file_get_html('https://twitter.com/UserName');
$tweets = $html->find('div.ProfileTweet-contents');
foreach ($tweets as $tweet) {
$tweetText = $tweet->find('p.ProfileTweet-text', 0)->plaintext;
echo $tweetText;
}
?>
... which is fine for getting the tweet text but I don't know how to approach getting that Unix timestamp.
I thought maybe:
<?php
include 'simple_html_dom.php';
$html = file_get_html('https://twitter.com/UserName');
$tweets = $html->find('div.ProfileTweet-contents');
foreach ($tweets as $tweet) {
$tweetText = $tweet->find('p.ProfileTweet-text', 0)->plaintext;
$tweetDate = $tweet->find('span.js-short-timestamp ', 0);
echo $tweetText.' '.$tweetDate->data-time;
?>
... but that's all wrong. Any help?
Most likely because of that property that you're trying to access. Wrapped that hypenated property with this:
$tweetDate->{'data-time'};
Rough example:
$html = file_get_html('https://twitter.com/katyperry');
$tweet_block = $html->find('div.ProfileTweet');
foreach($tweet_block as $tweet) {
// get tweet text
$tweetText = $tweet->find('p.ProfileTweet-text text', 0)->innertext;
echo 'Tweet: ' . $tweetText . '<br/>';
// get tweet stamp
$tweetDate = $tweet->find('a.ProfileTweet-timestamp span.js-short-timestamp', 0);
echo 'Timestamp: ' .$tweetDate->{'data-time'} . '<br/>';
echo '<hr/>';
}

Trim characters from RSS feed

I'm calling in a RSS feed to my website using PHP. Currently my code below is calling in the entire contents for pubDate:
<pubDate>Thu, 12 Sep 2013 07:23:59 +0000</pubDate>
How do I just display the day and month from the above example i.e. 12 Sep?
EDIT
I should clarify, the above line of code is an example output I currently get but as I'm calling the latest 3 posts from an RSS feed, this date and time will vary. I therefore need the code to be more dynamic (if that's the right term!)
This code is my full code that fetches the contents of an RSS feed:
<?php
$counter = 0;
$xml=simplexml_load_file("http://tutorial.world.edu/feed/");
foreach ($xml->channel->item as $item) {
$title = (string) $item->title; // Title Post
$link = (string) $item->link; // Url Link
$pubDate = (string) $item->pubDate; // date
$description = (string) $item->description; //Description Post
echo '<div class="display-rss-feed"><a href="'.$link.'" target="_blank" title="" >'.$title.' </a><br/><br/>';
echo $description.'<hr><p style="background-color:#e4f;">'.$pubDate.'</p></div>';
if($counter == 2 ) {
break;
} else {
$counter++;
}
} ?>
Use strtotime and date:
$pubDate = 'Thu, 12 Sep 2013 07:23:59 +0000';
$pubDate = date('j M', strtotime($pubDate)); //This is the only one you need!
var_dump($pubDate); //string(6) "12 Sep"
You can parse the date using date_parse and then use the values of month and day in the resulting array.
you can use preg_match() function with desired regular express to fetch particular data.
for example
$content="Thu, 12 Sep 2013 07:23:59 +0000";
preg_match("/.*,(. *)20[0-9][0-9]/"," $content",$g_val) ;
$g_val[1] would have " 12 Sep"
Even this works
<?php
$str="<pubDate>Thu, 12 Sep 2013 07:23:59 +0000</pubDate>";
$str=explode(" ",$str);
echo $str[1]." ".$str[2];//12 Sep
EDIT:
<?php
$counter = 0;
$xml=simplexml_load_file("http://tutorial.world.edu/feed/");
foreach ($xml->channel->item as $item) {
$title = (string) $item->title; // Title Post
$link = (string) $item->link; // Url Link
$pubDate = (string) $item->pubDate; // date
$pubDate=explode(" ",$pubDate);
$pubDate = $pubDate[1]." ".$pubDate[2];
$description = (string) $item->description; //Description Post
echo '<div class="display-rss-feed"><a href="'.$link.'" target="_blank" title="" >'.$title.' </a><br/><br/>';
echo $description.'<hr><p style="background-color:#e4f;">'.$pubDate.'</p></div>';
if($counter == 2 ) {
break;
} else {
$counter++;
}
} ?>

return rss node based on date

I am trying to return twitter titles based on today's date only. I have made the following code below, but it returns every title no matter if its today's date or not.
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=google');
$xml = simplexml_import_dom($dom);
$twitter = $xml->xpath("//item");
foreach ($twitter as $item) {
$timezone = new DateTimeZone('America/Los_Angeles');
$date = new DateTime($item->pubdate);
$date->setTimeZone($timezone);
$twitter_date = $date->format("F j Y");
$todays_date = date("F j Y");
if ($twitter_date == $todays_date) {
foreach ($twitter as $item) {
$text = $item->title;
echo $text.'<br />';
}
}
}
You are looping again through EVERY $twitter inside the if statement. Try removing the foreach tag inside and just using the current $item:
if ($twitter_date == $todays_date) {
$text = $item->title;
echo $text.'<br />';
}

Time since pubDate

In a new project, I'm working with rss being read by PHP and displayed on a page.
One thing I'd like to do is show how much time has passed since the post was published, but I can't find a way to do so, this is my current code, hope somebody can help me!
echo "<div id=\"left\">";
$rss1 = new DOMDocument();
$rss1->load('http://www.macfan.nl/macfan.rss');
echo '<h2>MacFan</h2>';
$feed = array();
foreach ($rss1->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$date = date('F d', strtotime($feed[$x]['date']));
echo '<p>'.$title.'</p>';
echo "<p class=\"small\">$date</p>";
}
echo "</div>";
You can compare the unix timestamps;
$seconds_between_now_and_then=(time()-strtotime($feed[$x]['date']));
Then you can see how far it is apart. These below could help you make it more readable for yourself:
$minutes_between_now_and_then=$seconds_between_now_and_then/60;
$hours_between_now_and_then=$minutes_between_now_and_then/60;
$days_between_now_and_then=$minutes_between_now_and_then/24;
echo 'Seconds:'.$seconds_between_now_and_then.'<br />';
echo 'Minutes:'.$minutes_between_now_and_then.'<br />';
echo 'Hours:'.$hours_between_now_and_then.'<br />';
echo 'Days:'.$days_between_now_and_then;

Categories