say i wanted to find out what day the second saturday of august is (just happens to be the day im getting married)... could go for anyday of any month for any year.. what would that look like?

i do know i need the date returned as: date("n j")

    Well the first Saturday in August (this year) is strtotime('Saturday August').... you can '+1 week' to get the Saturday after that.

      <?php
      /**
      * int nth_day_of_month(int $nbr, str $day, int $mon, int $year)
      *   $nbr = nth weekday to find
      *   $day = full name of weekday, e.g. "Saturday"
      *   $mon = month 1 - 12
      *   $year = year 1970, 2007, etc.
      * returns UNIX time
      */
      function nth_day_of_month($nbr, $day, $mon, $year)
      {
         $date = mktime(0, 0, 0, $mon, 0, $year);
         if($date == 0)
         {
            user_error(__FUNCTION__."(): Invalid month or year", E_USER_WARNING);
            return(FALSE);
         }
         $day = ucfirst(strtolower($day));
         if(!in_array($day, array('Sunday', 'Monday', 'Tuesday', 'Wednesday',
               'Thursday', 'Friday', 'Saturday')))
         {
            user_error(__FUNCTION__."(): Invalid day", E_USER_WARNING);
            return(FALSE);
         }
         for($week = 1; $week <= $nbr; $week++)
         {
            $date = strtotime("next $day", $date);
         }
         return($date);
      }
      // USAGE:
      echo date("n j", nth_day_of_month(2, 'saturday', 8, 2007));
      ?>
      

        Awesome... i like em both 🙂

        much obliged gentlemen

          @ NOGDOG, how could i specify the last day, like, the last friday of a month?

            Just set a $date variable to the first day of the next month, then get the "last friday", something like:

            $date = mktime(0, 0, 0, $month + 1, 1, $year);
            $lastFriday = strtotime('last Friday', $date);
            
              scrupul0us wrote:

              i cant believe its that simple... graci

              strtotime() rocks. 🙂

                Write a Reply...