Handy function for 2600 meeting websites: getNextFirstFriday()

Being the lazy sodhacker that I am, I decided I’d automate the display of the next meets date on the sidebar, after struggling with PHP’s strtotime and trying to get it to do what I wanted it to, I gave up and nabbed a bit of code off PHX2600, but it irked me that it used so much looping to do something that strtotime should be easily able to do, at least in theory.
I revisited it again last night and came to the conclusion that my difficulties were in the way strtotime handles date strings as being “from the current timestamp”, even when trying to get it for the next month.
So the answer to this was to generate timestamps for the very beginning of a month and then pipe this through strtotime.
With this out of the way, I present getNextFirstFriday():

<?php
/**
* Return a unix timestamp for next "first Friday of the month", e.g.
* either the first Friday of the current month or if that date has already passed
* the first friday of the next month
* @return int unix timestamp
*/
function getNextFirstFriday() {
$firstFridayThisMonth = strtotime('first friday', mktime(0, 0, 0, date('n'), 0, date('Y')));
if (time() < strtotime('7pm', $firstFridayThisMonth)) {
return $firstFridayThisMonth;
}
return strtotime('first friday', strtotime(date('Y-m-0', strtotime('next month'))));
}
?>

As you can see it’s obviously a PHP function. It doesn’t use any version-specific features so if you’re absolutely crazy you should be able to run it under PHP 4.x.
You’ll probably want to use it with something like date() to output a meaningful string from the unix timestamp it outputs, e.g. echo date('Y-m-d', getNextFirstFriday());

Although it’s such a small snippet that most would normally consider it trivial and thus needing no copyright notice, I’ll just say it’s licensed under The Simplified BSD License for prosperity.

 1

Next meeting will be:
Friday 1st October 2010 @ 7pm
The Brewery Tap Leeds


IRC


/server irc.freenode.net
/join #leeds2600

User