php code to find the 2nd wed of each month.

php code to find the 2nd wed of each month.

Posted by: Sirgeekalot
Posted on: 2009-03-04 03:27:00

i host a meeting on the 2nd wed of every month, and rather than change the date on my main page every month manually (currently stored in a text file), i was wondering if someone could help me with some code to do it automatically.

many thanks,

adam

Re: php code to find the 2nd wed of each month.

Posted by: Venom792001
Posted on: 2009-03-28 01:40:00

You can get a lot of useful tips about time, date etc from

http://us2.php.net/manual/en/function.date.php

Re: php code to find the 2nd wed of each month.

Posted by: pangea33
Posted on: 2009-03-28 23:58:00

I decided to mess with this just to see what was involved. It wasn't that complicated but it took more steps than I originally anticipated. Someone else could probably find a more efficient way but since no one suggested a solution, try this:

Output looks like this: Wednesday, March 11, 2009

<?php
  $mon = date("m");
  $year = date("y");
  // find the first weds of this month which will be within 7 days
  for ($dayofmonth=1; $dayofmonth<=7; $dayofmonth++) {
    $dateArr = getdate(mktime(0, 0, 0, $mon, $dayofmonth, $year));
    if ( !strcmp($dateArr["weekday"], "Wednesday") ) {
      // advance to following wednesday
      $dayofmonth += 7;
      break;
    }
  }
  $secWeds = date("l, F d, Y", mktime(0, 0, 0, $mon, $dayofmonth, $year));
  print($secWeds);
?>

Tags: many thanks