获得本月当前周

时间:2016-12-05 13:36:16

标签: php

Bellow code返回一个月内的No of Weeks。我如何找到本月的当前周数。

function weeks_in_month($year, $month, $start_day_of_week)
  {
    // Total number of days in the given month.
    $num_of_days = date("t", mktime(0,0,0,$month,1,$year));

    // Count the number of times it hits $start_day_of_week.
    $num_of_weeks = 0;
    for($i=1; $i<=$num_of_days; $i++)
    {
      $day_of_week = date('w', mktime(0,0,0,$month,$i,$year));
      if($day_of_week==$start_day_of_week){
        $num_of_weeks++;
        //$this->getStartAndEndDate($num_of_weeks,$month,$year);
      }

    }
    //return $week_start;
    return $num_of_weeks;

  }

1 个答案:

答案 0 :(得分:0)

``

/**
 * Returns the amount of weeks into the month a date is
 * @param $date a YYYY-MM-DD formatted date
 * @param $rollover The day on which the week rolls over
 */
function getWeeks($date, $rollover)
{
    $cut = substr($date, 0, 8);
    $daylen = 86400;

    $timestamp = strtotime($date);
    $first = strtotime($cut . "00");
    $elapsed = ($timestamp - $first) / $daylen;

    $weeks = 1;

    for ($i = 1; $i <= $elapsed; $i++)
    {
        $dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
        $daytimestamp = strtotime($dayfind);

        $day = strtolower(date("l", $daytimestamp));

        if($day == strtolower($rollover))  $weeks ++;
    }

    return $weeks;
}


//
echo getWeeks("2011-06-11", "sunday"); //outputs 2, for the second week of the month

``

来源:PHP get number of week for month

相关问题