按职位获取日期(即1月的第三个星期三)?

时间:2012-09-19 13:44:48

标签: php

我可以使用strtotime获取第三个星期三的第三个星期三(“结婚年月+2周”),还是需要像这样的复杂代码:http://www.danielkassner.com/2010/05/22/get-date-by-position-ie-third-wednesday-of-january

2 个答案:

答案 0 :(得分:1)

试试这个: -

/**
* 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);
} 


$d = nth_day_of_month($week_no,$curr_week_day, $fmonth, $fyear);

echo date('d-m-Y',$d)."<br />";

答案 1 :(得分:1)

这是本月的第3个星期三。如果用户依赖,则更改$month的值。

编辑1:http://codepad.org/8OzgMQsA

编辑2:http://codepad.org/3Tn6Ypu2