список номеров недели за данный месяц в php - PullRequest
0 голосов
/ 21 февраля 2011

Я должен перечислить номера недель для заданных месяцев.

Допустим, мой ввод должен быть 2 (фев) мой вывод должен быть списком номера недели в фев (5, 6, 7)

, пожалуйста, помогите мне в этом

Спасибо

Ответы [ 2 ]

1 голос
/ 21 февраля 2011
$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);
0 голосов
/ 26 апреля 2019
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);}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...