php中给定月份的周数列表

时间:2011-02-21 11:50:36

标签: php date

我必须列出给定月份的周数。

让我说我的输入应该是2(feb) 我的输出应该是feb中的周数列表(5,6,7)

请帮助我

由于

2 个答案:

答案 0 :(得分:1)

$input = "2(feb)";

// parse the input string to extract the month
list(,$month) = sscanf($input,'%d(%[^)]s)');

// Get timestamp for the 1st day of the requested month (using current year)
$startMonth = strtotime('1-'.$month);
// Get the ISO week number for the 1st day of the requested month
$startWeek = date('W',$startMonth);

// Get timestamp for the last day of the requested month (using current year)
$endMonth = strtotime('+1 Month -1 Day',$startMonth);
// Get the ISO week number for the last day of the requested month
$endWeek = date('W',$endMonth);

// get a range of weeks from the start week to the end week
if ($startWeek > $endWeek) {
    // start week for january in previous year
    $weekRange = range(1,$endWeek);
    array_unshift($weekRange,intval($startWeek));
} else {
    $weekRange = range($startWeek,$endWeek);
}

var_dump($weekRange);

答案 1 :(得分:0)

function getWeekRange($month=1){
  $dt = new \DateTime('first Monday of this month');
  $startWeek = date('W',strtotime($dt->format('Y-m-d')));
  $currentWeek = date('W');
  $weeks_in_month = weeks_in_month($month,date('Y'));
  $endWeek = $startWeek+($weeks_in_month-1);



  $weekRange =array();
  for($i=$startWeek;$i<=$endWeek;$i++){

    $weekRange[] = $i;
  }
  print_r($weekRange);}