I’m developing a RSS fetcher that grab my blogs and my friend’s blogs using SimplePie. Apparently I’m having problem with the RSS dates, the dates are less accurate because of time zone difference.
When it tested at the localhost, the date of the RSS articles appeared no trouble, but when I run the application on the live server, the date adjusted to the local on the server. I live in Indonesia that use UTC+7 time zone whereas my live server located in U.S. that differ -13 hours from Indonesia.
Initially I set the server time zone to Indonesia time zone in RSS fetcher code segment using date_default_timezone_set(‘Asia/Jakarta’); and it works! The RSS date were adjusted into Indonesia time zone.
This thing works fine, because I read Indonesian blogs or blog written by Indonesian people who live in Indonesia, even their blogs were hosted outside Indonesia. The blog settings usually has been set into local time (Indonesia time zone), so time zone difference is not a big deal anymore.
But lately, I realize that this step applies only temporary and locally. What if the RSS blog is located on a server with a different time zone setting (to my local and my live server)? It would be easier to make adjustment when the date is in GMT/UTC time zone.
SimplePie’s get_date() function fetches RSS pub date using local time zone setting. It’s similar to native PHP date() function.
So, how do I get the GMT/UTC date since SimplePie doesn’t provide function like native PHP gmdate() function?
You can do this on your code:
$gmtdate = gmdate('j M Y H:i \G\M\T', $item->get_date('U'))
That will result something like this:
9 Sep 2009 02:46 GMT
The “U” parameter in get_date() function told SimplePie to return seconds in Unix Epoch format. In other word, it means we get a GMT/UTC date.
Because of RSS pub date are in RFC 2822 format, we can easily get the GMT/UTC date format without worry pub date are using GMT or GMT+timezone. This date format will be transformed into same Unix Epoch timestamp.
It’s nice to using SimplePie to make a custom RSS Fetcher. But unfortunately I have some bad experience with SimplePie, especially long running time to merge, sort and process entire RSS. It’s not happen in localhost, but happen in real server. As for date, we the blog owner must be aware for the datestamp in pubDate tag that provided by RSS engine. In Indonesia, do not forget to add +0700 in the end of date, for example Wed, 09 Sep 2009 00:37:45 +0700 .
What version of SimplePie did you use? I think SimplePie is the fastest RSS fetcher and it parses XML nicely. Have you activate the cache? Caching is a great feature that you have to activate to make your apps work lighter and faster.
FYI, I store all data on a database. The fetcher sequent runs once an hour using crontab. It is more efficient than you run it live. Of course it will save your server resources and bandwidth. I fetch hundreds feeds and it works fine.
For date, this is why I post this. Storing data in GMT/UTC is more flexible to adjust. By setting the timezone option (set to Asia/Jakarta) the date will be appeared correctly. :)