First Friday script updated and simplified

This is an update to the previous post found here: http://www.web-geek.net/posts/2008/10/20/calculate-the-first-friday-of-next-month-with-php/

With this update I have drastically reduced and simpified the code to produce the same results.  I did some rigorous testing of my own to make sure this script will calculate the correct date, but that doesn’t mean it’s bullet-proof.  If you find a bug, please email me so I can fix it.

first-friday.php

<?php // first-friday.php v0.1.3 by, Chris Kankiewicz <http://www.web-geek.net>
 
  // Calculate next Friday 
  for ($x = date('d'); $x <= (date('d') + 6); $x++) {
    $timeStamp = mktime(0,0,0,date('m'),$x,date('Y'));
    if (date('w',$timeStamp) == 5) {
      $nextFriday = mktime(0,0,0,date('m'),$x,date('Y'));
    }
  }
  // Check if next Friday is the first friday of the month.
  if (date('d', $nextFriday) <= 7) {
    $firstFriday = $nextFriday;
  } else {
  // Calculate first Friday of next month
    for ($x = 1; $x <= 7; $x++) {
      $timeStamp = mktime(0,0,0,date('m')+1,$x,date('Y'));
      if (date('w',$timeStamp) == 5) {
        $firstFriday = mktime(0,0,0,date('m')+1,$x,date('Y'));
      }
    }
  }
 
  // Echo next first Friday
  echo date("F j, Y", $firstFriday);
 
?>

Calculate the first Friday of next month with PHP

UPDATE: This script has been updated, see: http://www.web-geek.net/posts/2009/02/14/first-friday-script-updated-and-simplified/

While developing phx2600.org, I ran into a slight dilemma.  The PHX2600 meetings occur once a month on the first Friday of every month, and we wanted to display that on the site.  However, it was becoming a tedious chore to change the date once a month manually.  So, being the automation addict I am, I thought, why not write a script.  So one night I hammered out the following script that will calculate the first Friday of next month:

Read the rest of this entry »