1

I want to design a PHP script for determine list of all the Monday of the month.

For December - 2014 (1-12-2014, 8-12-2014, 15-12-2014, 22-12-2014, 29-12-2014) and

For January - 2015 (29-12-2014, 5-1-2015, 12-1-2015, 19-1-2015, 26-1-2015)

For February - 2015 (2-2-2015,9-2-2015,16-2-2015,23-2-2015)

For November - 2014 (27-10-2014,3-11-2014,10-11-2014,17-11-2014,24-11-2014,)

In Script if the first day of the month is middle of the week than it should count the last month Monday.

In this script week starts from Monday to Sunday.

Ans :

<?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;
?>

Thanks All of you.

Parshva Mehta
  • 161
  • 2
  • 9

2 Answers2

7

You can make a function that will create a \DateTime object at the first monday of the month. Then in a while loop iterate over the days (in 7 day increments) and clone the \DateTime object until the next month is reached.

With this function you can also specify the day that wish to build an array collection of.

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

Then when you run..

$days = getAllDaysInAMonth(2015, 01);

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

You will end up with..

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

NOTE $daysError section added so as to fit the caveat of needing the previous months last specified day if start of the month is past "mid-week".

qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • when i gives ** getAllDaysInAMonth(2015, 01); ** then the Monday of the 29-12-2014 does not appers – Parshva Mehta Jan 29 '15 at 12:11
  • That's not in January of 2015 or in 2015 at all. That's in December of 2014. – qooplmao Jan 29 '15 at 12:14
  • but i need that week of Monday, i could not get that 29-12 Monday so i uploaded this question – Parshva Mehta Jan 29 '15 at 12:15
  • 1
    But it's not in the month. How do you decide which extra mondays go into a "month"? Are you looking for 5 mondays with the last in the month? You would need to elaborate more on your specific criteria. If you just want an array of those days then the best way would be to hard code it. – qooplmao Jan 29 '15 at 12:18
  • no no you getting wrong, all the week of month For December - 2014 (1-12-2014, 8-12-2014, 15-12-2014, 22-12-2014, 29-12-2014) and For January - 2015 (29-12-2014, 5-1-2015, 12-1-2015, 19-1-2015, 26-1-2015) AND For Fabuary -2015 ( 2-2-2015,9-2-2015,16-2-2015,23-2-2015) **if the start day is mid day of the week than that week should include** – Parshva Mehta Jan 29 '15 at 12:25
  • 1
    Mid day of the week being which day (does you week go Sun to Sat, Mon to Sun, ...)? You should also include any caveats in your question so it makes sense. – qooplmao Jan 29 '15 at 13:20
  • week goes to Mon to Sun. I think i give example of two months that is enough for the under standing the question – Parshva Mehta Jan 29 '15 at 13:23
  • It's clearly not enough understanding for the question as you have had 2 answers that give you pretty much the same answer but then you had to give further information above. Clearly your original question didn't give enough details as you have had to clarify it further. – qooplmao Jan 29 '15 at 13:53
  • i will take care of your suggestion, Thank you – Parshva Mehta Jan 30 '15 at 08:34
  • I also updated my answer. – qooplmao Jan 30 '15 at 10:03
1

The Below function will give you mondays in a month

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;
} 
texelate
  • 2,460
  • 3
  • 24
  • 32
Veerendra
  • 2,562
  • 2
  • 22
  • 39