[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help parsing and integrating Tumblr RSS feed using PHP

Posted on 2012-08-31
9
Medium Priority
?
2,320 Views
Last Modified: 2013-11-18
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
Comment
Question by:egoselfaxis
  • 4
  • 4
9 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38354209
0
 

Author Comment

by:egoselfaxis
ID: 38354312
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
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38354497
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:egoselfaxis
ID: 38354586
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
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38354654
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
 

Author Comment

by:egoselfaxis
ID: 38354783
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
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 1800 total points
ID: 38355317
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 38357835
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
 

Author Closing Comment

by:egoselfaxis
ID: 38360775
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question