Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get dates for all sundays in a month

Tags:

php

I want to be able to come up with a php function that takes in the parameters year, month and the day and returns the dates for the given day in an array.

for eg. lets say the function looks like this:

function get_dates($month, $year, $day)
{
    ....
}

if I call the function as below:

get_dates(12, 2011, 'Sun');

I should get an array containing the values:

2011-12-04
2011-12-11
2011-12-18
2011-12-25

What would the function code look like?

like image 896
user765081 Avatar asked Nov 17 '25 05:11

user765081


1 Answers

Here is the sample

function getSundays($y,$m){ 
    $date = "$y-$m-01";
    $first_day = date('N',strtotime($date));
    $first_day = 7 - $first_day + 1;
    $last_day =  date('t',strtotime($date));
    $days = array();
    for($i=$first_day; $i<=$last_day; $i=$i+7 ){
        $days[] = $i;
    }
    return  $days;
}

$days = getSundays(2016,04);
print_r($days);
like image 177
Jerald Avatar answered Nov 19 '25 21:11

Jerald