Advertisement
Fearlessagent

getSecondTuesday

Feb 2nd, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1.     function getSecondTuesday($test_date = NULL){
  2.         if ($test_date == NULL) {$current_date = array(date("n",time()), date("j",time()), date("Y",time()));}
  3.         else {$current_date = array(date("n",$test_date), date("j",$test_date), date("Y",$test_date));}
  4.         var_dump($test_date);
  5.         $result_date = $current_date;
  6.         $month_begin_weekday = date("w",mktime(0,0,0,$current_date[0],1,$current_date[2]));
  7.         if (self::debug) echo "First day of the month is: ".$month_begin_weekday."<br/>\n";
  8.         if($month_begin_weekday==2){
  9.             if (self::debug) echo "First day of the month is the first Tuesday of the month. Second Tuesday is in exactly one week.<br/>\n";
  10.             $result_date[1] = date("j",mktime(0,0,0,$current_date[0],1,$current_date[2])) + 7;
  11.         }
  12.         elseif($month_begin_weekday>2){
  13.             if (self::debug) echo "This week's Tuesday not require_onced in this month. FF a little less than two weeks.<br/>\n";
  14.             $result_date[1] = date("j",mktime(0,0,0,$current_date[0],1,$current_date[2])) + (14 - ($month_begin_weekday-2));
  15.         }
  16.         else{
  17.             if (self::debug) echo "This week's Tuesday is the first Tuesday of the month. FF a little more than one week.<br/>\n";
  18.             $result_date[1] = date("j",mktime(0,0,0,$current_date[0],1,$current_date[2])) + (7 + (2-$month_begin_weekday));
  19.         }      
  20.         return $result_date;
  21.     }
  22.  
  23.     static public function getNextSecondTuesday($testDate = NULL){
  24.         if ($testDate == NULL) {$testDate = time();}
  25.         if (self::debug) {
  26.             echo "Date to be tested: <br/>\n";
  27.             print_r($testDate);
  28.             echo "<br/>\n";
  29.         }
  30.         $result = getSecondTuesday($testDate);
  31.         if ($result[1] < $testDate[1]){
  32.             if (self::debug) echo "This month's meeting is passed, get Second Tuesday for next month.<br/>\n";
  33.             $testDate[0]++;
  34.             $result = getSecondTuesday($testDate);
  35.         }
  36.        
  37.         $the_result = mktime(18,0,0,$result[0], $result[1], $result[2]);
  38.        
  39.         return $the_result;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement