Calendar - 1st and 3rd Friday of Month

Discussion in 'PHP' started by LazyD, Feb 10, 2007.

  1. #1
    Hey All, im currently working on a project and I am using PHP Calendar which is a great script by the way. Ive modified bits and pieces to do what I need it to but im not sure how I would implement the following:

    A class is held every 1st and 3rd Friday of every month, instead of doing it manually every month I would like to find a solution to have it automatically create those classes on the calendar.

    Does anyone know of a way to do such a thing?
     
    LazyD, Feb 10, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    <?php
    
    $search = 0;
    
    for ($i = 1; $i < 31; $i++)
    {
    	$day = mktime(0, 0, 0, date('n'), $i);
    	
    	if (date('N', $day) == 5 AND in_array(++$search, array(1, 3)))
    	{
    		echo 'The '. date('dS M', $day) ." is a friday.<br />\n";
    	}
    }
    
    ?>
    
    PHP:
    Gets the first and third friday of the current month.


    EDIT:

    You can also make a function of this.
    
    function get_fridays_of($month = false, $year = false, $fridays = array(1, 3))
    {
    	if (!$month)
    	{
    		$month = date('n');
    	}
    	if (!$year)
    	{
    		$year = date('Y');
    	}
    	
    	$search = 0;
    	$matches = array();
    	
    	for ($i = 1; $i < 31; $i++)
    	{
    		$day = mktime(0, 0, 0, $month, $i, $year);
    		
    		if (date('N', $day) == 5 AND in_array(++$search, $fridays))
    		{
    			$matches[] = $i;
    		}
    	}
    	
    	return $matches;
    }
    
    
    PHP:
    And get the fridays of a specific month/year.

    Example:

    
    $fridays = get_fridays_of(4, 2003); // Gets the first and third friday of april 2003
    
    PHP:
     
    nico_swd, Feb 10, 2007 IP