Grab all Wednesdays in a given month in PHP

前端 未结 11 1640
长发绾君心
长发绾君心 2020-12-09 18:36

This is the function I\'m trying to write:

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


        
相关标签:
11条回答
  • 2020-12-09 19:03

    There's probably a more efficient way to do it, but this works:

    <?php
    function isWednesday($day, $month, $year)
    {
        if (date('w', $date = mktime(0,0,0,$month,$day,$year)) == 3) {
            return $date;
        }
        return false;
    }
    function getWednesdays($month, $year)
    {
        for ($day=1; $day<=7; $day++) {
            if ($date = isWednesday($day, $month, $year)) {
                break;
            }
        }
        $wednesdays = array();
    
        while (date('m',$date) == $month) {
            $wednesdays[] = $date;
            $day += 7;
            $date = isWednesday($day, $month, $year);
        }
        return $wednesdays;
    }
    

    You can test with this:

    foreach (getWednesdays($argv[1], $argv[2]) as $date) {
        echo date("Y-m-d\n", $date);
    }
    
    $ php wednesdays.php 11 2010
    2010-11-03
    2010-11-10
    2010-11-17
    2010-11-24
    $ php wednesdays.php 12 2010
    2010-12-01
    2010-12-08
    2010-12-15
    2010-12-22
    2010-12-29
    $ php wednesdays.php 2 2012
    2012-02-01
    2012-02-08
    2012-02-15
    2012-02-22
    2012-02-29
    

    S

    0 讨论(0)
  • 2020-12-09 19:04

    I have written following code for this purpose ... and its working perfectly for me

    function get_month_date($year, $month, $day) {
    $monthdays = date('t', mktime(0, 0, 0, $month, 1, $year));
    $dates = array();
    for($i = 0; $i <= $monthdays; $i++ ) {
        if($day == date('l', mktime(0, 0, 0, $month, 1+$i, $year)) && $month == date('n', mktime(0, 0, 0, $month, 1+$i, $year))) {
        $dates[] = date('Y-m-d', mktime(0, 0, 0, $month, 1+$i, $year));
        }
    }
    return $dates; }
    

    We can use it as :

    $days = get_month_date($year, $month, 'Wednesday');
    

    It will return array of all dates of Wednesday in given month and year

    0 讨论(0)
  • 2020-12-09 19:18

    Try This one to get all Wednesdays in current month.

    $month  = date('m');
    $year  = date('Y');
    function getDays($year,$month){ 
     $days = cal_days_in_month(CAL_GREGORIAN, $month,$year);
     $wed = array();
     for($i = 1; $i<= $days; $i++){
     $day  = date('Y-m-'.$i);
     $result = date("D", strtotime($day));
     if($result == "Wed"){  
     $wed[] = date("Y-m-d", strtotime($day)). " ".$result."<br>";
     }  
    }
     return  $wed;
    }
    $wed = getDays($year,$month);
    print_r($wed);
    
    0 讨论(0)
  • 2020-12-09 19:19

    If DateTime is not given, you need to know the day for one date - start of unix era for example (1st Jan 1970).

    Going from there, you simply locate the first wednesday, and from there find your month. Keep in mind the leap years, and you're good.

    Obviously, it isn't the most efficient algorithm in the world, but it would do the job.

    0 讨论(0)
  • 2020-12-09 19:20

    I have re-written @Gordon's solution to suit for any day

    function get_dates_of_day($y, $m, $day_name)
    {
        return new DatePeriod(
            new DateTime("first $day_name of $y-$m"),
            DateInterval::createFromDateString("next $day_name"),
            new DateTime("next month $y-$m-01")
            );
    }
    

    Now you can simply do get_dates_of_day('2015','08','wednesday') and use the same to get dates for any day in a given month.

    0 讨论(0)
  • 2020-12-09 19:21

    With PHP earlier than 5.3 this solution doesn't work. Seems, First Day Time stamp is greater than the last day time stamp, which eventually terminate the loop in first go.

    As PHP calculates time differences at midnight, this solution does not work when the month starts on the day in question. And for some cases does not work for the last day either.

    I've sorted out these problems in following code. And this code can be used for any day of week.

    The solution was created with the help of Eli's code in this question: Get the First or Last Friday in a Month

    ///// code starts here /////
    
    function getWeekDates($year, $month, $day){
    $tsInMonth = strtotime("$year-$month");
    $monthNumber = (int)date("m",$tsInMonth);
    
    $Names = array( 1=>"Sun", 2=>"Mon", 3=>"Tue", 4=>"Wed", 5=>"Thu", 6=>"Fri", 7=>"Sat" );
    
    $ThisMonthTS = strtotime( date("Y-m-01", $tsInMonth ) );
    $NextMonthTS = strtotime( date("Y-m-01", strtotime("next month", $tsInMonth) ) );
    
    $weekDates = array();
    
    for($Ord=1;$Ord<=5;$Ord++){
        $DateOfInterest = (-1 == $Ord) 
            ? strtotime( "last ".$Names[$day], $NextMonthTS ) 
            : strtotime( $Names[$day]." + ".($Ord-1)." weeks", $ThisMonthTS );
    
        ($Ord==5 && (int)date("m",$DateOfInterest)==(int)date("m",$NextMonthTS))
            ? false
            : $weekDates [] = $DateOfInterest;
    }
    
    return $weekDates;
    }
    
    ///// Usage /////
    
    foreach (getWeekDates("2010", "2", "2") as $day) {
        echo "<br>" . date("l, Y-m-d", $day);
    }
    
    ///// End of code /////
    

    I hope it helps someone.

    0 讨论(0)
提交回复
热议问题