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

用PHP抓取给定月份的所有星期三,php,datetime,Php,Datetime,这就是我要写的函数: function getWednesdays($month, $year) { // Returns an array of DateTimes representing all Wednesdays this month. } 有什么想法吗?谢谢-如果没有给出日期时间,您需要知道一个日期的日期-例如unix时代的开始(1970年1月1日) 从那里开始,你只需找到第一个星期三,然后从那里找到你的月份。记住闰年,你很好 显然,它不是世界上最有效的算法,但它可以完成这项

这就是我要写的函数:

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

有什么想法吗?谢谢-

如果没有给出日期时间,您需要知道一个日期的日期-例如unix时代的开始(1970年1月1日)

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


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

可能有一种更有效的方法,但这是可行的:

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

您可以尝试以下方法:

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;
}
使用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");
}
foreach (getWednesdays(2010, 11) as $wednesday) {
    echo date("l, Y-m-d\n", $wednesday);
}
输出:

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;
}
输出与上面相同()

注意,这是由于相对格式解析器中的更改造成的。如果您想在PHP5.3+中使用它,您必须将
第一个星期三$y-$m-01
更改为
第一个星期三$y-$m-01
(注意“of”)。此外,与DateTime版本一样,结束日期将不包括在内


进一步阅读:

  • ()


但是,如果您可以使用PHP5.3的日期和时间函数,我强烈建议您习惯它们。(参见Gordon的答案。)

对于早于5.3的PHP,此解决方案不起作用。看起来,第一天的时间戳大于最后一天的时间戳,这最终会在第一次运行时终止循环

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

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

在这个问题上,该解决方案是在Eli代码的帮助下创建的:

///code从这里开始/////
函数getWeekDates($year、$month、$day){
$THINMOUNT=STROTIME($year-$MOUNT”);
$monthNumber=(int)日期(“m”和$month);
$Names=array(1=>“Sun”,2=>“Mon”,3=>“Tue”,4=>“Wed”,5=>“Thu”,6=>“Fri”,7=>“Sat”);
$ThisMonthTS=strottime(日期(“Y-m-01”,每月$s));
$NextMonthTS=strotime(日期(“Y-m-01”),strotime(“下个月”,“每月”);
$weekDates=array();

对于($Ord=1;$Ord,我已经为此编写了以下代码……它对我来说非常适合

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

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

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

    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($year、$month、$day){
$from=$year.“-”$MOUNT.“-01”;
$t=日期(“t”,标准时间($from));

对于($i=1;$i 2014-08-04[1]=>2014-08-11[2]=>2014-08-18[3]=>2014-08-25),我已经重新编写了@Gordon的解决方案,以适应任何一天

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")
        );
}
现在您只需执行
获取日期('2015'、'08'、'周三')
并使用相同的方法获取给定月份中任何一天的日期。

以下是示例

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);
函数getSundays($y,$m){
$date=“$y-$m-01”;
$first_day=日期('N',标准时间($date));
$first_day=7-$first_day+1;
$last_day=日期('t',标准时间($date));
$days=array();
对于($i=$first_day;$i
尝试此选项可以获取当月的所有星期三

$month=日期('m');
$year=日期('Y');
函数getDays($year,$month){
$days=每月的计算天数(计算公历,$month,$year);
$wed=array();

对于($i=1;$ii如果您有可用的DateTime,这很容易。检查我认为他正在查找给定月份的所有星期三,由月份和年份值指定。我知道:)…上述算法的第一部分与在给定月份到达第一个星期三的过程有关。如果没有给出任何其他信息,则必须有某种“锚定”日开始with@playcat-你误解了这个问题-请看其他答案…我可以发誓它最初没有说DateTime…你确定你没有编辑这个问题吗?我评论了你的问题,建议如果DateTime可用,任务很简单…@playcat-问题没有编辑-如果是,你会看到,所有对SO的编辑都是可见的另一个非常好的答案,但你是对的,Gordon正在充分利用PHP的现有功能。如果我在2014年1月的每个星期五返回,我会得到4个星期五,不包括最后一个星期五每月的一天,周五。有什么想法吗?@LukeOliff它不包括结束日期。你必须在其中添加一天,才能包含它。请参阅更新。是的,谢谢。我在这里提出了一个问题:我只需要添加一秒钟,但仅用一分钟以防万一:)2015年9月(2015年,9日),上星期三不返回,总共有5个星期三,但只返回4个。@15121991注意这段话“请注意,这将排除结束日期,因此如果$y-$m的最后一天恰好是星期三,它将不在列表中。您必须在结束日期中添加一天以将其包括在内。要包括它,请将结束日期中的相对格式更改为
新日期时间(“下个月$y-$m-01”)
$days = get_month_date($year, $month, 'Wednesday');
    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;
 }
    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;
 }
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")
        );
}
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);
$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);