here is my code so far:

<?php 
//year and month
function getMondays($year) { 
  $newyear = $year; 
  $week = 0; 
  $day = 0;
  //month in this case december
  $mo = 12; 
  $mondays = array(); 
  $i = 1; 
  //whilst numerical day of the week is not equal to 1 which is the monday
  //purpose of the loop is to get the current date and set the $day to the first monday
  while ($week != 1) { 
    //keep incrementing by 1 until you hit a monday
    $day++; 
    //december 30th is a wednesday so it falls under the 3rd day of the week
   $week = date("w", mktime(0, 0, 0, $mo,$day, $year)); 
   echo $week.'<br>';

  } 

  //pushes the date found onto the end of the array
  array_push($mondays,date("r", mktime(0, 0, 0, $mo,$day, $year))); 

  return $mondays; 
} 

I can so far get it to get the first monday in the month, however I am pretty confused on how to get the rest of the mondays in a month, and then sticking that onto an array called $mondays;

All help appreciated.

    Just keep adding 7 days per week until next 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) &#37; 7;
    	$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;
    }
    
      johanafm;10978589 wrote:

      Just keep adding 7 days per week until next 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) % 7;
      	$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;
      }
      

      Thanks

        johanafm;10978589 wrote:

        Just keep adding 7 days per week until next 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) % 7;
        	$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;
        }
        

        Could you please clarify this for me

        	$addDays = (8 - $firstDay) % 7;
        	$mondays[] = date('r', mktime(0, 0, 0, $month, 1 + $addDays, $year))
        

        Don't really know what's going on here.

        Cheers

          Write a Reply...