SimplePie RSS Reader

If you’re thinking about adding syndication to your website, consider using SimplePie, a super fast and easy-to-use feed parser written in PHP. With SimplePie, adding a RSS feed to your site couldn’t be more easier. In this article, I will show you how to create a RSS page that will display a feed. It will display my blog’s RSS feed as default when nothing is clicked. I’ve included a couple of links that are clickable. When the links are clicked, the RSS feed for that link will be displayed.

First, download SimplePie.

Next, we need to create a file and copy the code below. The code is pretty much self-explanatory.

// include SimplePie
// Create a new object called $feed
$feed = new SimplePie();
// Set the default to my blog's RSS at first run
if ($_GET[feed] == "") { $_GET[feed]=""; }
// Run SimplePie.
// Send the result to show up on the browser
<!-- Display the links -->
<a href="rssfeed.php?feed=">Yahoo News</a> | 
<a href="rssfeed.php?feed=">Reuter News</a>
<!-- Display the header -->
<div class="header">
  <h1><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a></h1>
  <p><?php echo $feed->get_description(); ?></p>
// Display the feed inside a loop
foreach ($feed->get_items() as $item): ?>
<div class="item">
<li><a href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo $item->get_title(); ?></a></li>
  <p><?php echo $item->get_description(); ?></p>
  <p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
/* end of file */

See the demo. Just add your own CSS stylesheet and you’re ready to go.

Calculating Dates in PHP

Calculating and formatting dates is an important function when writing software applications. You might be needing to perform a database search of all transactions a month ago, six months ago, etc. To make this all possible, you need to be able to figure out how to calculate dates based from today’s date. PHP has a couple of date functions called date() and mktime(). I use the date() function primarily for formatting dates, and mktime() function for calculating dates since mktime() is the actual Unix timestamp. With these two functions, you can do some pretty amazing date calculations.

Let’s get started.

// Today's date
$day   = date("d");
$month = date("m");
$year  = date("Y");
// Using mktime calculate past dates
$start_of_three_months_ago = mktime(0, 0, 0, date("m")-2, 1, date("Y"));
$end_of_three_months_ago   = mktime(23, 59, 59, date("m")-1, 0, date("Y"));
// Using mktime to calculate future dates
$start_of_three_months_from_now = mktime(0, 0, 0, date("m")+3, 1, date("Y"));
$end_of_three_months_from_now   = mktime(23, 59, 59, date("m")+4, 0, date("Y"));
// Formatting dates to 'month day, year' format.
$a = date("M j, Y", $start_of_three_months_ago);
$b = date("M j, Y", $end_of_three_months_ago);
$c = date("M j, Y", $start_of_three_months_from_now);
$d = date("M j, Y", $end_of_three_months_from_now);
// results are based on today's date 
echo $day;
// results in 26
echo $month;
// results in 02
echo $year;
// results in 2014
echo $a;
// results in Dec 1, 2013
echo $b;
// results in Dec 31, 2014
echo $c;
// results in May 1, 2014
echo $d;
// results in May 31, 2014

You can then use any set of dates to calculate transactions on the database. For example.

SELECT price FROM product WHERE purchase_date BETWEEN $a AND $b;

Read more about PHP’s date and mktime functions. See demo. Results are based on today’s date.

The Latest Speed Test

I recently switched to Comcast for my Internet services. It has been several years since I have left and abandoned Comcast. I was curious more than anything as to what kind of download speeds I’ll be getting now that I’m back to using broadband cable. The result is pretty impressive. It has been a while since I’ve seen this kind of speeds at home. It’s about 4-5 times faster than what I previously had. I ran 3 different speed tests from 3 different sources. I’m a little surprised the results were similar. All fell within the same range of one another. So, without much further ado, here are the results of the speed tests.

Comcast Speed Test






URL Spaces in CodeIgniter

I ran into some issues with a piece of code I wrote several years ago in CodeIgniter. I’m using names of people as part of the URL. When someone enters a name with spaces on the database, the spaces are converted to %20 in the resulting URL. The URL doesn’t look pretty and definitely not SEO friendly. It needs to be cleaned up by replacing spaces with either a dash or an underscore.

The other issue is when performing a search, the database doesn’t really know that ‘John Doe’ is essentially the same as ‘John%20Doe.’ So, I ended up using a CodeIgniter function called url_title. The function strips the spaces and replaces it with a dash or a hyphen. In the case of ‘John%20Doe’, it now displays as ‘John-Doe.’ You can also use an underscore. See example below.

Replace %20 with dash or hypen

$name = 'John Doe';
$url = url_title($name);
// produces 'John-Doe'
$name = 'John Doe';
$url = url_title($name, '_');
// produces 'John_Doe'

Put the space before the search

Before performing a search, I use str_replace to remove the dash and put back the space so it matches the database entry. It’s essentially the reverse of what I did to display a friendly URL.

$url = 'John-Doe';
$name = str_replace('-', ' ', $url);
// perform your search function here

If you’re not familiar with CodeIgniter, check how URI routing and url_title are handled.

Importing SQL in phpMyAdmin

If you have a problem importing SQL files into phpMyAdmin, the issue could be that the upload limit in PHP is set too low. By default, PHP sets the upload file limit to 2MB, which is too low for most people. You can increase the limit to something more realistic such as 16MB, if you’re working mostly with medium-sized SQL databases.

If you have access to the Terminal and have root access, you can edit your PHP.ini settings. First, you need to find out where your default PHP.ini is located. There might be several PHP.ini files in your system, but there’s only one bound to Apache. To determine to which one is being used, I suggest you create a file that contains the PHP function called phpinfo(). Place this file your web server and run it.

Create a file called phpinfo.php. Enter the code below. Save. Upload to server.

<?php phpinfo(); ?>

Now, open your browser and point the URL where your phpinfo.php is stored on your web server. The phpinfo.php file will run the phpinfo() function and will display the environment variables being used by Apache. Near the top of the page, you’ll see the path of the php.ini. Now that you know where your php.ini file is exactly located, you’ll need to edit that file and look for the Upload Limits.

In Ubuntu, my php.ini is located in /etc/php5/apache2/php.ini.

sudo nano /etc/php5/apache2/php.ini

Look for the File Uploads text as displayed below. Change from 2M to 16M. Save file.

; File Uploads ;
upload_max_filesize = 16M

Finally, you need to reboot Apache for your changes take effect.

sudo /etc/init.d/apache restart