March 14th, 2009
Distinct Variation(s): Graphic Design & Web Development
I finally put up a portfolio!
I finally put up a portfolio!
$when = date('F j, Y', strtotime($datetime));
The above code, where the ‘$datetime’ variable is a MySQL datetime stamp, will output a humanized date use PHP’s date(); function.
I found myself needing some excuses and was amazed that this site didn’t exist.
So I made it.
Please visit/participate.
I was looking for a way to keep the About section up to date and came across this one from Dzone that works really well:
function birthday ($birthday)
{
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
You can activate it with:
echo birthday("YYYY-MM-DD");
Due to a recent influx in contact form spam (not from here), I’ve been looking into ways to prevent the spam without sacrificing accessibility and usability (see this report for the problems resulting from CAPTCHAs). I came across this post on the WebAIM Blog which is pretty great. If you have intermediate-level PHP skills, there are a lot of simply-to-execute techniques that will help reduce your spam load without compromising your users.
A couple weekends ago, Travis and I got a little too obsessed with GiggleSugar’s “Who’s Douchier?” quiz. Travis ended up registering an account and we set about trying to get on the top 10 list.
Matthew: This is going nowhere. I think I know a way to figure out the actual rankings so we can cheat.
Travis: Yeah, I was thinking about writing them down and trying to figure it out, too.
Matthew: Write down? Like with paper and pencil? No no no, I mean I’m pretty sure I can make a little application that’ll do all that for us.
Travis: …what?
Matthew: Give me a few minutes and I’ll show you.
So I went about creating this:

The person on the left is the douchier one. So in this example, the battle was between Carlos Mencia and Chuck Norris. Mencia, obviously, is the douchier of the two so you put him on the left. Put Norris on the right, hit go, and it switches their positions on the list below. Theoretically, with enough input, the list will be exactly the same as the one from the quiz.
Even now, at it’s incomplete state, you can consistently score 20+. I was about to start writing an algorithm that actually saves the matches to prevent some colossally bad switches but, well, it’s a douche bag quiz. I wasn’t exactly motivated to go beyond the initial five minutes. But, here it is. Please feel free to use it and contribute to the cause.

My money is on Spencer taking the crown.
I added a “Random Post” widget to the sidebar today. I spent a couple minutes at the Wordpress Widgets site, but I couldn’t find anything I liked so I made my own in PHP that is stupidly simple.
I defined the randomPost() function in wp-includes/functions.php because I knew it was being included already. You can put it anywhere, or just dump the code directly onto your page.
function randomPost() {
<div class="side-sec pages">
<h3>Random Post</h3>
<?php
// Get ID, Title, Post, and Date from Wordpress DB
$query = "SELECT ID, post_title, post_content, post_date FROM wp_posts WHERE post_type='post' ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
// Use $row for grabbed content
$row = mysql_fetch_array($result);
// Take only the first 150 characters and get rid of HTML from post
$content = substr(strip_tags($row[2]), 0, 150);
// Display using relative dates
echo '<p><a href="?p='.$row[0].'">'.$row[1].'</a> '.lastfm_relative($row[3]).'</p>
<p>'.$content.' [...]</p>';
?>
</div>
}
Since I already use the Last.fm for Wordpress plugin, I used their relative time function: lastfm_relative(). If you don’t have/want that plugin, here is the code for the function.
function lastfm_relative($time) {
$time_orig = strtotime($time);
$diff = $just = time()-$time_orig;
$months = floor($diff/2592000);
$diff -= $months*2419200;
$weeks = floor($diff/604800);
$diff -= $weeks*604800;
$days = floor($diff/86400);
$diff -= $days*86400;
$hours = floor($diff/3600);
$diff -= $hours*3600;
$minutes = floor($diff/60);
$diff -= $minutes*60;
$seconds = $diff;
// Variables defined
if ($just<=0) {
return 'Just Now!';
} else {
if ($months>0) {
// over a month old, just show date (yyyy/mm/dd format)
return 'on '.date('Y/m/d', $time_orig);
} else {
if ($weeks>0) {
// weeks and days
$relative_date .= ($relative_date?', ':'').$weeks.' '.__('week').($weeks>1?'s':'');
$relative_date .= $days>0?($relative_date?', ':'').$days.' '.__('day').($days>1?'s':''):'';
} elseif ($days>0) {
// days and hours
$relative_date .= ($relative_date?', ':'').$days.' '.__('day').($days>1?'s':'');
$relative_date .= $hours>0?($relative_date?', ':'').$hours.' '.__('hour').($hours>1?'s':''):'';
} elseif ($hours>0) {
// hours and minutes
$relative_date .= ($relative_date?', ':'').$hours.' '.__('hour').($hours>1?'s':'');
$relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' '.__('minute').($minutes>1?'s':''):'';
} elseif ($minutes>0) {
// minutes only
$relative_date .= ($relative_date?', ':'').$minutes.' '.__('minute').($minutes>1?'s':'');
} else {
// seconds only
$relative_date .= ($relative_date?', ':'').$seconds.' '.__('second').($seconds>1?'s':'');
}
}
}
// show relative date and add proper verbiage
return $relative_date.' ago';
}
And that’s it. The relative time function is by far the most complex part of this whole operation. There’s no validation or verification — it assumes that there are posts ready to be grabbed, that your database is accessible, etc. You can see it working on the right side of this page.
It’s rare for me to do any kind of digital work without a paper/pencil or whiteboard next to me for jot notes, drafts, and calculations. I’ve been redeveloping the entry system for a dictionary that’s going to be used by hundreds of students around the world for the next seven years, and eventually as the public digital front end. It’s very important to me that I get this right.
Draft 1 was just a quicky to make sure I could get everything I wanted, so there was no drawing. Now that is IS working, it’s time to think about usability and aesthetics — the two most important aspects, to me, of any website — and that means it’s time to grab a marker.

Draft 2 was an attempt to streamline the dictionary’s entry system, but failed when it came down to semantics. The back-end was ridiculously elaborate and difficult. A system does not work when the person who created it can’t figure out how to use it properly.

Draft 3 looks a little cramped from the wire frame, but it’s very intuitive and slick — this is it for now. Also, it has big ass Submit and Cancel buttons. Failing something awful, the final product will probably be very similar.
I’ve gotten rid of the To Do List on the right side of this website. I realize that I’m not the type of person who responds well to pressure — internal or external — as motivation. With the exception of the first week, I was only adding things that I didn’t really care about (i.e. whether or not I’ll finish a book within the alloted time) or I could make vague enough not to matter (i.e. “The Web 2.0 Project”; what does that even mean?!). And in truth, most of the things I do I either CAN’T or WON’T tell people about until they’re actually done.
If anyone remembers and wants it, send me an email. It’s simple PHP and MySQL.
The big loser? Well, anyone who writes apps for social networks, pretty much by definition.