Hi guys!
In previous article we discuss how to Calculate the number of Second Saturady, Fourth Saturday and Sunday Between Two dates in PHPIn this article share how to print or display the second saturday,fourth saturday and all sunday between two date range unsing php.
Consider from date and two date for performing this operations. The output is may be one element or more than one element. So we using the array for storing and printing the result.
Some important tips or points are used for finding the solutions. They are:
- Calculating the week number for a given date – PHP
- Day of the Date
- Print second saturday, fourth saturdayand all sunday
Based on the above steps or points we can reach the solution. Because, we get the day of the date then we get the day is sunday or saturday. After that we can find day of the week. If the week is 2 then we can say it’s second week the we can find the second saturday.If the week is 4 then we can find the fourth saturday. And also we can find all sundays.
Calculating the week number for a given date – PHP
Getting the week number of given date in month using php. It’s very simple and useful for finding the second or fourth week between two date range. Its very useful for fetching all second saturday or fourth saturday from calender.
In the above code the output is:4. 22nd august is fourth week of 2017.
Day of the Date -PHP
The day of the date in php is check the date of the day. We can print all the day, between two date.
format('l');
echo $daydate;
?>
The output of the above code is:Thursday.
Print second saturday, fourth saturdayand all sunday
Fetch all second saturday,fourth saturday and all sunday between two date using pure php. Its very simple, because we get the day of the date, and also week of the date. If the week is 2 and day is saturday then we get the second saturday. Based on this method we get fouth saturday, sunday between the two date range.
Full Code
format('l');
if($daydate=='Sunday')
{
$daydate= 0;
}
else if($daydate=='Monday')
{
$daydate= 1;
}
else if($daydate=='Tuesday')
{
$daydate= 2;
}
else if($daydate=='Wednesday')
{
$daydate=3;
}
else if($daydate=='Thursday')
{
$daydate= 4;
}
else if($daydate=='Friday')
{
$daydate=5;
}
else
{
$daydate= 6;
}
if ($week_day == 2)
{
if ($daydate == 6)
{
$total++;
$date_details=date("Y-m-d",$j);
$e2=array();
$e2['title']='Second Saturday ';
$e2['start']=$date_details;
array_push($events,$e2);
}
}
if($week_day == 4)
{
if ($daydate == 6)
{
$total++;
$date_details=date("Y-m-d",$j);
$e3=array();
$e3['title']='Fourth Saturday ';
$e3['start']=$date_details;
array_push($events,$e3);
}
}
if ($daydate == 0)
{
$total++;
$date_details=date("Y-m-d",$j);
$e4=array();
$e4['title']='Sunday';
$e4['start']=$date_details;
array_push($events,$e4);
}
}
echo 'total second saturday fourth saturday and sunday:'.$total;
echo '
';
echo json_encode($events);
?>
The output is:
total second saturday fourth saturday and sunday:10
[{“title”:”Second Saturday “,”start”:”2017-07-08″},{“title”:”Sunday”,”start”:”2017-07-09″},{“title”:”Sunday”,”start”:”2017-07-16″},{“title”:”Fourth Saturday “,”start”:”2017-07-22″},{“title”:”Sunday”,”start”:”2017-07-23″},{“title”:”Sunday”,”start”:”2017-07-30″},{“title”:”Sunday”,”start”:”2017-08-06″},{“title”:”Second Saturday “,”start”:”2017-08-12″},{“title”:”Sunday”,”start”:”2017-08-13″},{“title”:”Sunday”,”start”:”2017-08-20″}]
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.