Reputation: 2860
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 ?
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/
but i think its the long way to do so. so any quick trick? even in mysql
Upvotes: 0
Views: 4910
Reputation: 5283
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
}
}
Upvotes: 1
Reputation: 208
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>";
}
Upvotes: 1
Reputation: 3536
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;
}
Upvotes: 0
Reputation: 837
Try this one.You can print the date by this way.You can replace the task echo $day
as you want.
$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);
Upvotes: 2
Reputation: 13728
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
Upvotes: 0