First Friday script updated and simplified

NOTE: The latest version of this script can always be found here: http://github.com/PHLAK/first-friday/blob/master/first-friday.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

 
  // 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);
 
?>
This entry was posted in Programming, Scripting, Web Development and tagged , , , , . Bookmark the permalink.

2 Responses to First Friday script updated and simplified

  1. Pingback: Calculate the first Friday of next month with PHP | Web Geek

  2. maxwell says:

    This reminds me of how hideous php code is, haha. Nice script though!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">