How to get all Wednesdays of a Month PHP

A PHP function on how to retrieve a certain day from the week like Wednesday. Grab every Wednesday from the week.

The other week i had to figure out how to get all wednesdays from a week. While browsing online i found something on stackoverflow that i think might help you guys writting this function. 

With PHP5.3

function getWednesdays($y, $m)
{
    return new DatePeriod(
        new DateTime("first wednesday of $y-$m"),
        DateInterval::createFromDateString('next wednesday'),
        new DateTime("last day of $y-$m")
    );
}

 

Usage

foreach (getWednesdays(2010, 11) as $wednesday) {
    echo $wednesday->format("l, Y-m-d\n");
}

 

With PHP<5.3

function getWednesdays($y, $m)
{
    $ts  = strtotime("first wednesday $y-$m-01");
    $end = strtotime("last wednesday $y-$m");
    $wednesdays = array();
    while($ts <= $end) {
        $wednesdays[] = $ts;
        $ts = strtotime('next wednesday', $ts);
    }
    return $wednesdays;
}

Usage:

foreach (getWednesdays(2010, 11) as $wednesday) {
    echo date("l, Y-m-d\n", $wednesday);
}

Full Article: http://stackoverflow.com/questions/4293174/grab-all-wednesdays-in-a-given-month-in-php