Grab all Wednesdays in a given month in PHP

前端 未结 11 1641
长发绾君心
长发绾君心 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:23

    You can try something like this:

    function getWednesdays($month, $year) {
        $base_date = strtotime($year . '-' . $month . '-01');
        $wed = strtotime('first wed of ' . date('F Y', $base_date));
    
        $wednesdays = array();
    
        do {
            $wednesdays[] = new DateTime(date('r', $wed));
            $wed = strtotime('+7 days', $wed);
        } while (date('m', $wed) == $month);
    
        return $wednesdays;
    }
    
    0 讨论(0)
  • 2020-12-09 19:24
    <?php
    function wednesdays($month, $year)
    {
      list($n,$d) = explode('-', date('t-d', strtotime("Wednesday, $year-$month")));
      $days = array();
      while ($d <= $n)
      {
        $days[] = sprintf("%04d-%02d-%02d", $year, $month, $d);
        $d += 7;
      }
      return $days;
    }
    
    var_dump(wednesdays(11, 2010));
    
    ?>
    

    But I highly recommend getting used to PHP 5.3's date and time functions if you can use them. (See Gordon's answer.)

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

    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);
    
    0 讨论(0)
  • 2020-12-09 19:25

    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)
    0 讨论(0)
  • 2020-12-09 19:26

    Use this function to get any days total count in a month

        function getTotalDays($year, $month, $day){
            $from = $year."-".$month."-01";
            $t=date("t",strtotime($from));
    
            for($i=1; $i<$t; $i++){
               if( strtolower(date("l",strtotime($year."-".$month."-".$i)))== $day){
                 $count++;
              }
          }
          return $count;
     }
    

    use like this getTotalDays('2014', '08','monday');

    will output 4

    But if you want all dates in a month on a particular day then use this function

        function getTotalDatesArray($year, $month, $day){
            $date_ar=array();
            $from = $year."-".$month."-01";
            $t=date("t",strtotime($from));
    
            for($i=1; $i<$t; $i++){
               if( strtolower(date("l",strtotime($year."-".$month."-".$i)))== $day){
                $j= $i>9 ? $i: "0".$i;
                 $date_ar[]=$year."-".$month."-".$j;
              }
          }
          return $date_ar;
     }
    

    use like this getTotalDatesArray('2014', '08','monday');

    will output Array ( [0] => 2014-08-04 [1] => 2014-08-11 [2] => 2014-08-18 [3] => 2014-08-25 )

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