Category archive: php

September 19, 2007

Formatting date & time for RSS 0

According to the RSS spec, all of the dates in your RSS content should be in RFC 822 format. That means they should look like this:

Sat, 07 Sep 2002 09:42:31 GMT

Here’s a PHP function that will take a timestamp and give you that string:

function rss_time($time){
	return strftime("%a, %d %b %Y %H:%M:%S %z", $time);
}
August 12, 2007

I’m not proud of this 1

…but I didn’t have much of a choice:

function parse_date_from_string($string=""){
	$month_names = array("", "January","February","March","April","May","June","July","August","September","October","November","December");
	$regex = "/(\d{1,2})[-|\/|\.](\d{1,2})[-|\/|\.](\d{2}|\d{4})/";
	$date = array();
	if(preg_match($regex, $string, $matches)){
		$month = preg_replace("/^0/", "", $matches[1]);
		$date['month'] = $month_names[$month];
		$date['day'] = $matches[2];
		if(strlen($matches[3]) == 2) $matches[3] = "20" . $matches[3];
		$date['year'] = $matches[3];
		$date['string'] = trim(preg_replace($regex, "", $string));
	}
	return $date;
}
August 3, 2007

Super simple PHP gallery 2

Just save this as index.php and put it in a directory with a bunch of images. Images are displayed one per page (newest images first), and clicking on an image takes you to the next one.

<?
exec("ls -r|egrep -i \"\.(gif|jpg|jpeg|png)$\"", $files);
$files = array_reverse($files);
$request = $_SERVER["QUERY_STRING"];
if(!(is_numeric($request) && $request > 0 && $files[$request - 1])){
	$request = count($files);
}
$r_index = $request - 1;
$next = $r_index;
if($next==0 || !$files[$next-1]){
	$next = count($files);
}
?>
<html>
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8">
	<title>my image gallery</title>
 
	<style type="text/css" media="screen">
		a img{ border:0; }
	</style>
 
</head>
<body>
	<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
		<tr><td align="center" valign="middle"><a href="?<?=$next?>"><img src="<?=$files[$r_index]?>" /></a></td></tr>
	</table>
</body>
</html>
May 4, 2007

Bookflipper 0

Bookflipper is a tiny web app that serves up pricing information for used books from the Amazon API. My friend Brian has been picking up cheap books and selling them on Amazon for a profit. Now when he’s wondering if a purchase is going to pay off, he can just pull out his cell phone and enter the book’s ISBN into Bookflipper to see what the market is like.

Example 1
Example 2
Example 3

The page size is usually between 0.5-0.6k, thanks in part to some output buffer trickery. Of course, the preview image is much bigger but it seemed too useful to get rid of.

May 2, 2007

WordPress plugin: x-y of z 1

You can find it on the plugins download page now. This basically just gives you a function that will print out a snippet of text like “Viewing 1-10 of 423 posts” based on the contents of the page. Hopefully this is marginally useful to someone out there.