• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2428
  • Last Modified:

Need help parsing and integrating Tumblr RSS feed using PHP

I need to parse the following Tumblr RSS feed using PHP and it integrate the data into one of my client's websites (ie: title, link, date, photo & description):

http://gallerylife.tumblr.com/rss

Please advise on how I would go about this.  

Thanks,
- Yvan
0
egoselfaxis
Asked:
egoselfaxis
  • 4
  • 4
2 Solutions
 
Julian HansenCommented:
0
 
egoselfaxisAuthor Commented:
I already had a look at that link, and am already into it head first :)

http://www.antiqueplace.biz/tumblr.php

The problem that I'm having is that I can't seem to figure out how to format the date & time.  The date & time are currently stored in "$post->pubDate" .. but when I echo it as is .. I get the following format .. which isn't what I want:

Tue, 28 Aug 2012 11:41:06 -0400

The exact format that I want is ...

Tuesday, August 28, 2012 11:41 AM

.. which is just like the way the date & time are displayed in the RSS feed:

http://gallerylife.tumblr.com/rss

How can I format the date & time so that it's formatted the same as in the RSS feed?

Thanks,
- Yvan
0
 
Julian HansenCommented:
Here is some code that will do that
$date = 'Tue, 28 Aug 2012 11:41:06 -0400';
echo $date . "<br/>";
echo date('l, d M Y H:i:s O', strtotime($date)) . "<br/>";

Open in new window

The above will output the time basd on current timezone settings - it will still be in UTC format so it will still be the same time but the output may result in a different timezone than the one you are in. This is not an issue as UTC results in the same actual time.
If you want it output in a particular timezone then call  date_default_timezone_set() before doing the above and set to the timezone you want to output in.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
egoselfaxisAuthor Commented:
Hmm .. it's kinda/sorta working .. but not quite:

http://www.antiqueplace.biz/tumblr.php

(On my page -- the unformatted date is to the left of the hyphens, .. and the formatted date is to the right of the hyphens)

What's immediately noticeable -- aside from the full day of the week being displayed (instead of the abbreviated version - which is easy enough for me to fix) --- is that the AM/PM indicators aren't displaying.  

By the way -- calling "date_default_timezone_set();" had no effect whatsoever.

Here is my code:

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gallerylife.tumblr.com/rss');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($data);

foreach($xml->channel->item as $post) {

	echo '<p><strong><a href="' . $post->link . '" target="_blank">' . $post->title . '</a></strong><br />';
	
	date_default_timezone_set();
	
	$postDate = date('l, d M Y H:i:s O', strtotime($post->pubDate));
	
	echo $post->pubDate . ' ---- ' . $postDate . '</p>';
	
	echo '<p>' . $post->description . '</p>';
	
	echo '<hr />';
	
}

?>

Open in new window

0
 
Julian HansenCommented:
What's immediately noticeable -- aside from the full day of the week being displayed (instead of the abbreviated version - which is easy enough for me to fix)

Try this
date('D, d M Y H:i:s A', strtotime($post->pubDate));

Open in new window

That will do short weekday names - I though you wanted long names in case you do try this
date('l, d M Y H:i:s A', strtotime($post->pubDate));

Open in new window

0
 
egoselfaxisAuthor Commented:
That worked .. thank you!

http://www.antiqueplace.biz/tumblr.php

However, .. how do I alter the timezone value so that times match up?  

For example, the first post reads an original time of 11:41, .. whereas my formatted time displays as 9:41.  What timezone setting would I need to apply (and where) in order to get those timestamps to match up?

Thanks,
- Yvan
0
 
Julian HansenCommented:
Call this before hand

date_default_timezone_set()

You will need to pass in the correct parameter for your timezone though or the one one want to set it for.

But remember the time that comes out is calculated from UTC and the time you are creating is timezone specific so you need to make sure you are outputing the right value.
0
 
Ray PaseurCommented:
julianH is on the right track here.  The date/time issues are all covered in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
egoselfaxisAuthor Commented:
Thanks guys!  Here is my final script:

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gallerylife.tumblr.com/rss');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($data);

foreach($xml->channel->item as $post) {

	echo '<p><strong><a href="' . $post->link . '" target="_blank">' . $post->title . '</a></strong><br />';
	
	date_default_timezone_set("America/New_York");
	
	$postDate = date('l, F d, Y H:i A', strtotime($post->pubDate));
	
	echo $postDate . '</p>';
	
	echo '<p>' . $post->description . '</p>';
	
	echo '<hr />';
	
}

?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now