PHP中每月的每个星期一

时间:2015-01-29 11:02:03

标签: php

我想设计一个PHP脚本来确定该月的所有星期一的列表。

  

2014年12月(2014年1月12日,2014年8月12日,2014年12月15日,2014年12月22日,2014年12月22日,2014年12月29日)和

     

2015年1月( 29-12-2014 ,2015年5月1日,2015年12月1日,2015年1月19日,2015年1月26日,2015年1月26日)

     

2015年2月(2-2-2015,9-2-2015,16-2-2015,23-2-2015)

     

2014年11月 - ( 27-10-2014 ,3-11-2014,10-11-2014,17-11-2014,24-11-2014,)

在脚本中,如果该月的第一天是星期几,那么应该计算星期一的最后一个月。

在这个脚本中,周从星期一到星期日开始。

答:

<?php
 $selectedmonth="January 2015";
 $dat=strtotime("first day of ".$selectedmonth);        
    if(date('N',$dat)>1) {
        $previousmonth=date('F Y',strtotime($selectedmonth."-1 month"));
        $firstMonday=strtotime("last monday of ".$previousmonth);
    }
    else
    {
        $firstMonday=strtotime("first monday of ".$selectedmonth);
    }
    $temp=$firstMonday;
    $s="(".date("Y-m-d",$firstMonday).",";
    $lastmonday=strtotime("last monday of ".$selectedmonth);
    while($temp!=$lastmonday)
    {
        $temp=strtotime(date("d F Y",$temp)."+1 week");
        $s.=date("Y-m-d",$temp).",";
    }
    $s=trim($s,",").")";
    echo $s;
?>

谢谢你们所有人。

2 个答案:

答案 0 :(得分:6)

您可以创建一个在该月的第一个星期一创建\DateTime对象的函数。然后在while循环中迭代几天(以7天为增量)并克隆\DateTime对象,直到达到下个月。

使用此功能,您还可以指定希望构建数组集合的日期。

/**
 * Get an array of \DateTime objects for each day (specified) in a year and month
 *
 * @param integer $year
 * @param integer $month
 * @param string $day
 * @param integer $daysError    Number of days into month that requires inclusion of previous Monday
 * @return array|\DateTime[]
 * @throws Exception      If $year, $month and $day don't make a valid strtotime
 */
function getAllDaysInAMonth($year, $month, $day = 'Monday', $daysError = 3) {
    $dateString = 'first '.$day.' of '.$year.'-'.$month;

    if (!strtotime($dateString)) {
        throw new \Exception('"'.$dateString.'" is not a valid strtotime');
    }

    $startDay = new \DateTime($dateString);

    if ($startDay->format('j') > $daysError) {
        $startDay->modify('- 7 days');
    }

    $days = array();

    while ($startDay->format('Y-m') <= $year.'-'.str_pad($month, 2, 0, STR_PAD_LEFT)) {
        $days[] = clone($startDay);
        $startDay->modify('+ 7 days');
    }

    return $days;
}

然后当你跑...

$days = getAllDaysInAMonth(2015, 01);

foreach ($days as $day) {
    echo $day->format('D Y-m-d').'<br />';
}

你最终会得到......

Mon 2014-12-29
Mon 2015-01-05
Mon 2015-01-12
Mon 2015-01-19
Mon 2015-01-26

注意 $daysError部分,以便适应需要前一个月最后一个指定日期的警告,如果该月的开始已过去&#34;周中&#34;。

答案 1 :(得分:1)

“下方”功能将在一个月内为您提供星期一

function getMondays($year, $month)
{
    $mondays = array();
    # First weekday in specified month: 1 = monday, 7 = sunday
    $firstDay = date('N', mktime(0, 0, 0, $month, 1, $year));
    /* Add 0 days if monday ... 6 days if tuesday, 1 day if sunday
        to get the first monday in month */
    $addDays = (8 - $firstDay);
    $mondays[] = date('r', mktime(0, 0, 0, $month, 1 + $addDays, $year));

    $nextMonth = mktime(0, 0, 0, $month + 1, 1, $year);

    # Just add 7 days per iteration to get the date of the subsequent week
    for ($week = 1, $time = mktime(0, 0, 0, $month, 1 + $addDays + $week * 7, $year);
        $time < $nextMonth;
        ++$week, $time = mktime(0, 0, 0, $month, 1 + $addDays + $week * 7, $year))
    {
        $mondays[] = date('r', $time);
    }

    return $mondays;
} 
相关问题