简体   繁体   English

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

[英]get date of all sunday between two date PHP Mysql

What I want want to do is add task on every Sunday from date 1 to 31 of any month(or between any two random date),so how to get date of every Sunday ? 我想做的是在每个月的日期1到31的每个星期日 (或任意两个随机日期之间)添加任务,那么如何获取每个星期日的日期?

what i did for now is like: 现在所做的就像:

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

also posted here: http://wonderphp.wordpress.com/2014/09/18/add-recurring-task-php/ 也发布在这里: http : //wonderphp.wordpress.com/2014/09/18/add-recurring-task-php/

but i think its the long way to do so. 但是我认为这是很长的路要走。 so any quick trick? 那么有什么招数吗? even in mysql 即使在MySQL中

Try this one.You can print the date by this way.You can replace the task echo $day as you want. 尝试此操作。您可以通过这种方式打印日期。您可以根据需要替换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);

This will work for you 这对你有用

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

Edit : iF you want something more than do this 编辑:如果您想要做的比这更多

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

This might wil help you: 这可能会帮助您:

$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>";

}

Try 尝试

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

it will gives you all dates of sunday between two dates:- 它将为您提供两个日期之间的所有星期天日期:-

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 ) 

For more :- Find every Sunday between two dates 有关更多信息:- 查找两个日期之间的每个星期日

This will helps to find all sundays between two dates 这将有助于查找两个日期之间的所有星期日

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM