Monday 16 September 2019

PHP Tutorial | Navigate To Next And Previous Week & And Get Dates Within Week











Code : navigate_week.php
<center>


<a href="navigate_week_tutorial.php">Refresh</a>
<br/>
<?php
$week=0;
if(isset($_GET['week_count']))
{
$week=$_GET['week_count'];
}
else
{
$week=date('w');
}
echo "Today's Date :".date('Y-m-d'). " | Day : ".date('D');
echo "<br/>";
echo "First day of week : ".$week_first_date=date('Y-m-d',strtotime("-".(0+$week)." days"));
echo "| Day : ".date('D',strtotime($week_first_date));
echo "<br/>";
echo "Last day of week : ".$week_last_date=date('Y-m-d',strtotime("+".(6-$week)." days"));
echo "| Day : ".date('D',strtotime($week_last_date));
echo "<br/>";
$next_week_var=intval($week)-7;
$previous_week_var=intval($week)+7;
?>


<a href="navigate_week_tutorial.php?week_count=<?php echo $previous_week_var;?>"> << Previous Week</a> 

OR

<a href="navigate_week_tutorial.php?week_count=<?php echo $next_week_var;?>">Next Week >> </a>
<br/>
<?php
for($i=0;$i<=6;$i++)
{
echo $temp_date=date('Y-m-d',strtotime($week_first_date."+".$i." days"));
echo "| Day : ".date('D',strtotime($temp_date));
echo "<br/>";
}
?>
</center>

Please avoid or flag spams/hateful comments. And do not spam. Enjoy :)

Subscribe my Channel:-
http://www.youtube.com/user/SanketRooney?sub_confirmation=1

Facebook Page:-
https://www.facebook.com/UnpossibleNS

Twitter Account:-
https://twitter.com/UnpossiblePOG

Blog :-
https://unpossiblepog.blogspot.in/



 Hello guys.
In this tutorial, we
shall discuss how to navigate to each week, how to set range of
weekdays like from monday to sunday, or sunday to saturday, or any
from and to days you want ,and also how to get days of each week.
For example, let say
the current day is 15 sept 2019, so the range will always be of 7
days. But we get days from wednesday to tuesday.
We can also navigate
to next weeks or previous weeks and get dates between them.
So lets get started.
We shall build code
from the scratch.

First we shall get today’s date and day.
Next, I am going to
add refresh link, it’s purpose will be revealed later.
New we shall find
the week-day count using this code.
By using this, we
shall get first day and last of every week.
For first day, we
are using “minus” symbol in strtotime function.
And for last day of
week, we shll use plus sign.
Let get, which day
it is.
Now we shall find
all days between two days by using for loop.

Let make it on
center.
Now we shall add
week navigation, so fot that we have to find two numbers, for next
week and last week.
For previous week,
there will be + 7.
In href, we shall
make a variable called week count. And assign value for previous and
next week.
Now, even when
clicked on link, it doesn’t do anything.
So we have to
overwrite the $week value.
Let test
it.

Acutally I made a mistake in a code.
Ok, so you just have
to add + instead of minus at “Week first date”.
Lets refresh and
test, as you can see, we are hopping on each week.
But what if, we want
start date from monday. So change, like this and as you can see, the
result is coming correctly.
You can select from
day to any day like monday, tuesday wednesday.
So actually, the
main strategy is those numbers, we have to make sure that the
addition between them should be 6.

Let change current date to
17 september.
So no matter what
your current date will be, the code will display every day of every
previous and next weeks.
The code’s link is
in the description.
Thank you for
watching, dont forget to like share and subscribe.




1 comment: