代码之家  ›  专栏  ›  技术社区  ›  Yarin

用PHP获取给定月份的所有星期三

  •  11
  • Yarin  · 技术社区  · 14 年前

    这就是我要写的函数:

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

    有什么想法吗?谢谢-

    11 回复  |  直到 14 年前
        1
  •  28
  •   Gordon Haim Evgi    11 年前

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

    用法:

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

    输出:

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

    注意,这将排除结束日期,因此如果 $y-$m 碰巧是星期三,不在名单上。您必须在结束日期后添加一天,才能包含它。要包含它,请将结束日期中的相对格式更改为

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

    然后将结束日期设置为下个月的第一天,


    使用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;
    }
    

    用法:

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

    输出同上( Run on Codepad ).

    注意这个 does not work for any version prior to 5.3 由于相对格式分析器中的更改。如果要在PHP 5.3+中使用它,必须更改 first Wednesday $y-$m-01 first Wednesday of $y-$m-01 (注意“of”)。此外,与DateTime版本一样,结束日期将不包括在内。


    进一步阅读:

        2
  •  4
  •   Michal M    14 年前

    你可以试试这样的方法:

    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;
    }
    
        3
  •  3
  •   RN Kushwaha    10 年前

    使用此函数可获取一个月内的任何天数总数

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

    像这样使用getTotalDays('2014','08','monday');

    将输出4

    但是如果你想在一个月的某一天得到所有的日期,那么使用这个函数

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

    像这样使用getTotalDatesArray('2014','08','monday');

    将输出数组([0]=>2014-08-04[1]=>2014-08-11[2]=>2014-08-18[3]=>2014-08-25)

        4
  •  2
  •   scoates    14 年前

    也许有一种更有效的方法可以做到这一点,但这是可行的:

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

    你可以用这个测试:

    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公司

        5
  •  2
  •   Matthew    14 年前
    <?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));
    
    ?>
    

    但我强烈建议您使用PHP 5.3的日期和时间函数,如果您可以使用它们的话。(见戈登的回答。)

        6
  •  1
  •   Community CDub    8 年前

    对于早于5.3的PHP,这个解决方案不起作用。似乎,第一天的时间戳大于最后一天的时间戳,后者最终在第一个go中终止循环。

    当PHP在午夜计算时差时,当月份开始于所讨论的日期时,此解决方案不起作用。在某些情况下,最后一天也不起作用。

    我已经在下面的代码中解决了这些问题。这个代码可以用于一周中的任何一天。

    这个问题的解决方案是在Eli代码的帮助下创建的: 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 /////
    

    我希望它能帮助别人。

        7
  •  1
  •   Narendran Parivallal    9 年前

    我重新写了“戈登的解决方案”以适应任何一天

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

    现在你可以简单地 get_dates_of_day('2015','08','wednesday') 并用同样的方法获取给定月份中任何一天的日期。

        8
  •  0
  •   Rajen K Bhagat    11 年前

    为此我写了以下代码。。。它对我来说是完美的

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

    我们可以将其用作:

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

    它将返回给定月份和年份中所有星期三日期的数组

        9
  •  0
  •   Jerald    9 年前

    这是样品

    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);
    
        10
  •  0
  •   Kashif Sohail    8 年前

    试试这个,把本月所有的星期三都弄到手。

    $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);
    
        11
  •  -2
  •   hummingBird    14 年前

    如果没有指定日期时间,则需要知道一个日期的日期-例如unix纪元的开始日期(1970年1月1日)。

    从那里开始,你只需找到第一个星期三,然后从那里找到你的月份。记住闰年,你很好。

    显然,它不是世界上最有效的算法,但它可以完成这项工作。