获取两个日期PHP Mysql之间的所有星期日的日期

时间:2014-09-24 05:07:30

标签: php mysql

我想要什么 想要做的是在任何月份的日期1到31之间(或任意两个随机日期之间)在每个星期天添加任务,那么如何获取每个星期日的日期?

我现在做的就像:

for(i=start_date;i<end-date;i++ ){
    if(check i.th date == 'Sunday'){
       take date of i;
    }
}

也张贴在这里:http://wonderphp.wordpress.com/2014/09/18/add-recurring-task-php/

但我认为它还有很长的路要走。任何快速的伎俩?甚至在mysql中

5 个答案:

答案 0 :(得分:2)

试试这个。您可以通过这种方式打印日期。您可以根据需要更换任务echo $day

    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];

    $day = $start_date;
    do {
        list($day, $month, $year) = explode("-", $date);

    $wkday = date('l',mktime('0','0','0', $month, $day, $year));
        if($wkday == "sunday"){
        echo $day;
        }
        $day++;
    } while ($day < $end_date);

答案 1 :(得分:1)

这对你有用

$i=date("d",strtotime("today"));//your month first sunday like "january first sunday"    
while($i<30){
    i+=7;
}

编辑:我想要的不仅仅是这个

 $i=date("d",strtotime("today"));//your month first sunday like "january first sunday"    
 $sundays=array();
    while($i<30){
         array_push($sundays,$i);
         //set event
         i+=7;
     }

 for($j=$startdate;$j<$enddate;$j++){
     if($j ,$sundays){
    //add task
     }   
 }

答案 2 :(得分:1)

今晚会帮助你:

$start_date=strtotime("01 Sept 2014");
$end_date=strtotime("30 Sept 2014");
while(1){
  $start_date=strtotime('next sunday', $start_date);
  if($start_date>$end_date)
    break;
  echo "Next Sunday: ".date("d M Y",$start_date)."</br>";

}

答案 3 :(得分:0)

尝试

function getDateForSpecificDayBetweenDates($start, $end, $weekday = 0){
  $weekdays="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday";
  $arr_weekdays=split(",", $weekdays);
  $weekday = $arr_weekdays[$weekday];
  if(!$weekday)
    die("Invalid Weekday!");
  $start= strtotime("+0 day", strtotime($start) );
  $end= strtotime($end);
  $dateArr = array();
  $friday = strtotime($weekday, $start);
  while($friday <= $end) {
    $dateArr[] = date("Y-m-d", $friday);
    $friday = strtotime("+1 weeks", $friday);
  }
  $dateArr[] = date("Y-m-d", $friday);
  return $dateArr;
}
$dateArr = getDateForSpecificDayBetweenDates("Today", "+1 year", 0); //0 Sun, 1 Mon, etc.
print_r($dateArr);

它将为您提供两个日期之间的所有星期日日期: -

Array ( [0] => 2014-09-28 [1] => 2014-10-05 [2] => 2014-10-12 [3] => 2014-10-19 [4] => 2014-10-26 [5] => 2014-11-02 [6] => 2014-11-09 [7] => 2014-11-16 [8] => 2014-11-23 [9] => 2014-11-30 [10] => 2014-12-07 [11] => 2014-12-14 [12] => 2014-12-21 [13] => 2014-12-28 [14] => 2015-01-04 [15] => 2015-01-11 [16] => 2015-01-18 [17] => 2015-01-25 [18] => 2015-02-01 [19] => 2015-02-08 [20] => 2015-02-15 [21] => 2015-02-22 [22] => 2015-03-01 [23] => 2015-03-08 [24] => 2015-03-15 [25] => 2015-03-22 [26] => 2015-03-29 [27] => 2015-04-05 [28] => 2015-04-12 [29] => 2015-04-19 [30] => 2015-04-26 [31] => 2015-05-03 [32] => 2015-05-10 [33] => 2015-05-17 [34] => 2015-05-24 [35] => 2015-05-31 [36] => 2015-06-07 [37] => 2015-06-14 [38] => 2015-06-21 [39] => 2015-06-28 [40] => 2015-07-05 [41] => 2015-07-12 [42] => 2015-07-19 [43] => 2015-07-26 [44] => 2015-08-02 [45] => 2015-08-09 [46] => 2015-08-16 [47] => 2015-08-23 [48] => 2015-08-30 [49] => 2015-09-06 [50] => 2015-09-13 [51] => 2015-09-20 [52] => 2015-09-27 ) 

更多信息: - Find every Sunday between two dates

答案 4 :(得分:0)

这将有助于找到两个日期之间的所有星期日

$startdate='2014-05-17';
$enddate='2014-09-20';

getSundays($startdate,$enddate);

function getSundays($startdate,$enddate) {
    $startweek=date("W",strtotime($startdate));
    $endweek=date("W",strtotime($enddate));
    $year=date("Y",strtotime($startdate));

    for($i=$startweek;$i<=$endweek;$i++) {
        $result=getWeek($i,$year);
        if($result>$startdate&&$result<$enddate) {
            echo "Sunday:".$result."<br>";
        }
    }
}

function getWeek($week, $year) {
  $dto = new DateTime();
  $result = $dto->setISODate($year, $week, 0)->format('Y-m-d');
  return $result;
}