Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab all Wednesdays in a given month in PHP

Tags:

php

datetime

This is the function I'm trying to write:

function getWednesdays($month, $year) {
   // Returns an array of DateTimes representing all Wednesdays this month.
}

Any ideas? Thanks-

like image 957
Yarin Avatar asked Nov 27 '10 18:11

Yarin


People also ask

How get number of days in a month in php?

The cal_days_in_month() function returns the number of days in a month for a specified year and calendar.

How can I get first Sunday of the month in php?

Use getdate() or date('w') to learn about the weekday of the first day in the month then add or subtract the correct number of days and that's all. You don't need to check all the days in the week.

How can I get Saturday and Sunday of the month in php?

$weekends = array_filter($dates, function ($date) { $day = $date->format("l"); return $day === 'Saturday' || $day === 'Sunday'; });

How can I get first day of current month in php?

//get first day of the current month $start = date("Y-m-1 00:00:00"); //get current date of the month $end = date("Y-m-d H:i:s"); //query data for the current month so far $query = $this->db_model->run_query("select column_1, column_2 from table where date_column BETWEEN '".


1 Answers

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");
}

Output:

Wednesday, 2010-11-03
Wednesday, 2010-11-10
Wednesday, 2010-11-17
Wednesday, 2010-11-24

Note that this will exclude the end date, so if the last day of $y-$m happens to be a Wednesday, it won't be in the list. You have to add a day to the end date, to include it. To include it, change the relative format in the end date to

new DateTime("next month $y-$m-01")

which will then set the end date to the first day of the next month,


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);
}

Same output as above (Run on Codepad).

Note that this does not work for any version prior to 5.3 due to changes in the relative formats parser. If you want to use this with PHP 5.3+ you have to change first Wednesday $y-$m-01 to first Wednesday of $y-$m-01 (mind the "of"). Also, just like in the DateTime version, the end date will not be included.


Further reading:

  • DateTime in PHP Manual
  • Relative DateTime Formats
  • Derick Rethans: Advanced Date/Time Handling (ZendCon 10)
  • Relative item in date strings (mirror)
like image 186
Gordon Avatar answered Sep 25 '22 00:09

Gordon