PHP date_range() function

0
Filed under php
Tagged as , , , ,

A small function to return a date range based on a plain-English text description eg last week

//***** FUNCTION TO RETURN THE DATES FOR A GIVEN DATE RANGE IN MYSQL AND UK FORMAT *****
/**
	USAGE:
	$arr_dates = date_range("last month);

	EXAMPLE OUTPUT:
	Array
	(
	    [date_from] => Array
	        (
	            [mysql] => 2011-03-01
	            [uk] => 01/03/2011
	            [text_long] => Thursday 31st March 2011
	            [text_short] => 31 Mar 2011
	        )

	    [date_to] => Array
	        (
	            [mysql] => 2011-03-31
	            [uk] => 31/03/2011
	            [text_long] => Thursday 31st March 2011
	            [text_short] => 31 Mar 2011
	        )

	    [description] => Array
	        (
	            [range] => last month
	            [description] => March 2011
	        )

	)
 */
function date_range($period){
	$arr_allowed_date_ranges = array("yesterday", "this week", "last week", "this month", "last month", "this year", "last year", "all");
	if(!in_array($period, $arr_allowed_date_ranges)) return FALSE;

	//***** YESTERDAY *****
	if($period == "yesterday"){
		$mysql_date_from = $mysql_date_to = date("Y-m-d", mktime("0", "0", "0", date("m"), date("d")-1, date("Y")));
		$description = "Yesterday";

	//***** THIS WEEK *****
	}elseif($period == "this week"){
		$week = date("W");
		$i = 0;
		while(date('W', strtotime("-$i day")) >= $week) {
			$mysql_date_from = date('Y-m-d', strtotime("-$i day"));
			$i++;
		}
		$mysql_date_from = date('Y-m-d', strtotime("-$i day"));
        list($yr, $mo, $da) = explode('-', $mysql_date_from);
		$mysql_date_to = date('Y-m-d', mktime(0, 0, 0, $mo, $da + 6, $yr));
		$description = "This Week";

	//***** LAST WEEK *****
	}elseif($period == "last week"){
		$week = date("W") - 1;
		$i = 0;
		while(date('W', strtotime("-$i day")) >= $week) {
			$mysql_date_from = date('Y-m-d', strtotime("-$i day"));
			$i++;
		}
		$mysql_date_from = date('Y-m-d', strtotime("-$i day"));
        list($yr, $mo, $da) = explode('-', $mysql_date_from);
		$mysql_date_to     = date('Y-m-d', mktime(0, 0, 0, $mo, $da + 6, $yr));
		$description = "Last Week";

	//***** THIS MONTH *****
	}elseif($period == "this month"){
		$mysql_date_from   = date("Y-m-d", mktime("0", "0", "0", date("m"), "1", date("Y")));
		$mysql_date_to     = date("Y-m-d");
		$description = date("F Y", mktime("0", "0", "0", date("m"), "1", date("Y")));

	//***** LAST MONTH *****
	}elseif($period == "last month"){
		$mysql_date_from   = date("Y-m-d", mktime("0", "0", "0", date("m")-1, "1", date("Y")));
		$mysql_date_to     = date("Y-m-d", mktime("0", "0", "0", date("m"), date("t", mktime("0", "0", "0", date("m")-1), "1", date("Y")), date("Y")));
		$description = date("F Y", mktime("0", "0", "0", date("m")-1, "1", date("Y")));

	//***** THIS YEAR *****
	}elseif($period == "this year"){
		$mysql_date_from = date("Y-m-d", mktime("0", "0", "0", "1", "1", date("Y")));
		$mysql_date_to   = date("Y-m-d");
		$description = date("Y");

	//***** LAST YEAR *****
	}elseif($period == "last year"){
		$mysql_date_from = date("Y-m-d", mktime("0", "0", "0", "1", "1", date("Y")-1));
		$mysql_date_to   = date("Y-m-d", mktime("0", "0", "0", "12", "31", date("Y")-1));
		$description = date("Y", mktime("0", "0", "0", "12", "31", date("Y")-1));
	}

	if($mysql_date_from && $mysql_date_to){
		//***** FORMAT DATE RANGES IN UK FORMAT *****
		list($y,$m,$d)        = explode("-", $mysql_date_from);
		$uk_date_from         = "$d/$m/$y";
		list($y,$m,$d)        = explode("-", $mysql_date_to);
		$uk_date_to           = "$d/$m/$y";

		//***** FORMAT DATE RANGES IN LONGHAND FORMAT *****
		$text_long_date_from  = date("l jS F Y", mktime("0", "0", "0", $m, $d, $y));
		$text_long_date_to    = date("l jS F Y", mktime("0", "0", "0", $m, $d, $y));

		//***** FORMAT DATE RANGES IN SHORTHAND FORMAT *****
		$text_short_date_from = date("d M Y", mktime("0", "0", "0", $m, $d, $y));
		$text_short_date_to   = date("d M Y", mktime("0", "0", "0", $m, $d, $y));

		//***** BUILD THE ARRAY *****
		$arr_date_ranges = array("date_from"   => array("mysql"       => $mysql_date_from,
		                                                "uk"          => $uk_date_from,
		                                                "text_long"   => $text_long_date_from,
		                                                "text_short"  => $text_short_date_from
		                                               ),
		                         "date_to"     => array("mysql"       => $mysql_date_to,
		                                                "uk"          => $uk_date_to,
		                                                "text_long"   => $text_long_date_to,
		                                                "text_short"  => $text_short_date_to
		                                               ),
		                         "description" => array("range"       => $period,
		                                                "description" => $description
		                                               )
		                        );
	}else{
		$arr_date_ranges = array();
	}

	return $arr_date_ranges;
}

PHP multidate() function

0
Filed under php
Tagged as , , ,

I’m constantly looking up the reference page on php.net for the date() function, so instead I’ve written a small function to return the most commonly-used formats

//***** FUNCTION TO RETURN AN ARRAY OF FORMATTED DATE STRINGS *****
/**
	USAGE:
	$arr_dates = multidate("1301662294");

	EXAMPLE OUTPUT:
	Array
	(
	    [mysql] => 2011-04-01
	    [uk] => 01/04/2011
	    [text_long] => Friday 1st April 2011
	    [text_short] => 01 Apr 2011
	    [uk_with_time] => 01/04/2011 13:51:34
	    [text_long_with_time] => Friday 1st April 2011 1:51:34 pm
	    [text_short_with_time] => 01 Apr 2011 13:51:34
	)

 */
function multidate($timestamp){
	//***** IF NO TIMESTAMP PASSED, ASSUME TIME IS NOW *****
	if($timestamp == "") $timestamp = time();

	//***** MAKE SURE TIMESTAMP IS NUMERIC AND 10 DIGITS LONG *****
	if(!is_int($timestamp) && strlen($timestamp) != 10) return FALSE;

	//***** FORMAT OUR ARRAY *****
	$arr_dates = array("mysql"                => date("Y-m-d", $timestamp),
	                   "uk"                   => date("d/m/Y", $timestamp),
	                   "text_long"            => date("l jS F Y", $timestamp),
	                   "text_short"           => date("d M Y", $timestamp),
	                   "uk_with_time"         => date("d/m/Y H:i:s", $timestamp),
	                   "text_long_with_time"  => date("l jS F Y g:i:s a", $timestamp),
	                   "text_short_with_time" => date("d M Y H:i:s", $timestamp)
	);

	return $arr_dates;
}

CSS Goodness

0
Filed under php, Uncategorized

A complete rip-off of a page that was featured in b3ta a week back. I liked it so much I based an entire site on the concept.

I give you some CSS shit!

Installing Google’s Skipfish on Ubuntu

0
Filed under Linux
Tagged as , , ,
wget http://skipfish.googlecode.com/files/skipfish-1.01b.tgz
tar zxvf skipfish-1.01b.tgz
sudo apt-get install libidn11-dev
sudo apt-get install libssl-dev
cd skipfish
make
cp dictionaries/default.wl skipfish.wl
./skipfish -o output_folder http://www.example.com

Regular Expression for analysing ProFTPd’s xferlog

0
Filed under php
Tagged as , , ,
$xferlog_regex  = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun) ";                     //***** 1  Day
$xferlog_regex .= "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) "; //***** 2  Month
$xferlog_regex .= "(( |[0-3])[0-9]) ";                                  //***** 3  Date
$xferlog_regex .= "([0-9][0-9]:[0-5][0-9]:[0-5][0-9]) ";                //***** 5  Time
$xferlog_regex .= "(20[0-9][0-9]) ";                                    //***** 6  Year
$xferlog_regex .= "([^ ]*) ";                                           //***** 7  Transfer time
$xferlog_regex .= "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) ";  //***** 8  IP Address
$xferlog_regex .= "([^ ]*) ";                                           //***** 9  File size
$xferlog_regex .= "([^ ]*) ";                                           //***** 10 File name
$xferlog_regex .= "(a|b) ";                                             //***** 11 ASCII/Binary
$xferlog_regex .= "(C|U|T|_) ";                                         //***** 12 Compressed/Uncompressed/Tar'ed/No action
$xferlog_regex .= "(o|i|d) ";                                           //***** 13 Outgoing/Incoming/Deleted
$xferlog_regex .= "(a|g|r) ";                                           //***** 14 Authenticated/Guest/Real (local) user
$xferlog_regex .= "([^ ]*) ";                                           //***** 15 Username
$xferlog_regex .= "([^ ]*) ";                                           //***** 16 Service name
$xferlog_regex .= "(0|1) ";                                             //***** 17 Authentication: none/RFC931
$xferlog_regex .= "([^ ]*) ";                                           //***** 18 Authenticated User ID
$xferlog_regex .= "(c|i)";                                              //***** 19 Completed/Incomplete

or all in one go if you just want the regex:

/(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (( |[0-3])[0-9]) ([0-9][0-9]:[0-5][0-9]:[0-5][0-9]) (20[0-9][0-9]) ([^ ]*) ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) ([^ ]*) ([^ ]*) (a|b) (C|U|T|_) (o|i|d) (a|g|r) ([^ ]*) ([^ ]*) (0|1) ([^ ]*) (c|i)/

GeoIP Map Of Visitors

0
Filed under php
Tagged as

Just messing about with maxmind’s free GeoIP databases, and grabbed the logs for here to plot the IP addresses of visitors…

Adding twitter feeds to a site

0
Filed under Uncategorized

Just a bit of code I’ve come across that uses the Twitter API and is fairly fast

<ul id="twitter_update_list" style="width: 200px; "></ul>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/{__USERNAME__}.json?callback=twitterCallback2&count=all"></script>

PHP RSS Feed

0
Filed under php
Tagged as ,

I got sick of looking at shit pieces of code that either didn’t work properly, or produced XML so badly formatted you couldn’t read the raw source to debug it, so I decided to write my own, based on the BBC’s RSS source.

<?php
/*
 * @abstract   PHP script to correctly generate an RSS feed
 * @author     Jon Thompson
 * @version    0.1
 * @since      11 Dec 2009
 *
 * Requires an SQL table with the following fields to read:
 * - title
 * - link
 * - description
 * - thumbnail
 *
 * Will generate an on-the fly RSS feed from database-driven content.
 * For best results, amend the following and add to your pages in the <head></head> section:
 *
 * <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.yoursite.com/link/to/this/script/rss.php" />
 *
 */

//***** Database login details *****
$db_server       = "localhost";
$db_database     = "database";
$db_username     = "username";
$db_password     = "password";

//***** RSS feed details *****
$rss_title       = "Your title here";
$rss_link        = "URL to web page that displays these items";
$rss_description = "An overall description of your feed";
$rss_language    = "en-gb";
$rss_copyright   = "Your copyright notice";
$rss_docs        = "URL to a page explaining distribution and use of these articles";

//***** SQL query to generate list *****
$sql = "SELECT * FROM table limit 15";

//############################################################################################################
//# NOTHING FURTHER TO EDIT FROM THIS POINT ON                                                               #
//############################################################################################################

//***** Connect to database *****
$db = mysql_connect($db_server, $db_username, $db_password) or die("Unable to connect to MySQL server\n\n");
mysql_select_db($db_database, $db) or die("Unable to select database\n\n");

//***** Execute the SQL query *****
$result = mysql_query($sql, $db);

//***** Loop through the results to get the rest of the feed *****
if(mysql_num_rows($result)){
	while($row=mysql_fetch_assoc($result)){
		$output .= "		<item>
			<title>".htmlentities($row["title"])."</title>
			<link>".htmlentities($row["link"])."</link>
			<description>".htmlentities($row["description"])."</description>
			<media:thumbnail url=\"".htmlentities($row["thumbnail"])."\" />
		</item>
";
	}
}else{
	$output .= "		<item>
			<title>No entries</title>
			<link></link>
			<description>Sorry, there are no new entries to view</description>
			<media:thumbnail url=\"\" />
		</item>
";
}

//***** Build the entire feed from our results                                  *****
//***** This will also format it correctly so it's readble in plain text format *****
$output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
<?xml-stylesheet title=\"XSL_formatting\" type=\"text/xsl\" href=\"/shared/bsp/xsl/rss/nolsol.xsl\"?>
<rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss\">
	<channel>
		<title>$rss_title</title>
		<link>$rss_link</link>
		<description>$rss_description</description>
		<language>$rss_language</language>
		<lastBuildDate>".date("D, d M Y H:i:s T")."</lastBuildDate>
		<copyright>$rss_copyright</copyright>
		<docs>$rss_docs</docs>
		<ttl>15</ttl>
".$output."	</channel>
</rss>\n";

//***** Output the feed correctly *****
header("Content-Type: text/xml");
echo $output;
exit(0);
?>

Bwah-ha-ha-ha!

0
Filed under Football
Tagged as , , , ,

That brochure must have worked.

Those footballing giants Stoke and Hull are looking to be the favourites to sign Michael “committed, fit & healthy, successful” Owen.

Top class football to be had there.

WTF????

0
Filed under Football
Tagged as , , , ,
NUFC Away Kit 2009-2010

NUFC Away Kit 2009-2010

So here’s the away kit for next season, and I can only say…

What on earth were they thinking?

It’s bad enough that we were the laughing stock of the Premiership, now we’re going to be the biggest joke in the Mickey Mouse Chanmpionship as well.

I can only think that this is Mike Ashley’s final revenge for all the stick we’ve given him about his, shall we say, unique brand of ownership.

I mean, seriously. Yellow!!!??? Powder blue was bad enough, but yellow!

Well, that’s £40 that they won’t be getting from me this year. Expect to see this monstrosity in the sales come September.

Update 25th June 2009:

Well, it didn’t even make it till September. Out a week and already being flogged off for 20% off. What a surprise – no-one wants to buy it !