Link to home
Start Free TrialLog in
Avatar of egoselfaxis
egoselfaxis

asked on

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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Avatar of egoselfaxis
egoselfaxis

ASKER

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
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.
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

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

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
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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