Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to cycle through months in calendar

9:55 pm on Jan 14, 2016 (gmt 0)

New User

joined:Jan 14, 2016
posts: 3
votes: 0


I am trying to develop a diary booking system for my business - this is my first real project for a very long time.

I am adapting a calendar script I found on the internet which displays the calendar based on the current month which is taken from the server.

The calendar displays quite nicely but I would like to be able to cycle through previous and next months by clicking a link displayed either side of the date.

The variables I currently have available are:

$month = date("n");
$year = date("Y");
$firstDay = mktime(0,1,0,$month,1,$year);
$daysInMonth = date("t",$firstDay);
$firstDay = date("w",$firstDay);

and the "links" are displayed thus:

echo "<div class='section group'>";
echo "<div class='col span_1_of_3 prev'>Prev</div>\n";
echo "<div class='col span_1_of_3 current'>" . date("F Y") . "</div>\n";
echo "<div class='col span_1_of_3 next'>Next</div>\n";
echo "</div>";

I presume I will need to create a for loop but I have been struggling to get it to work.

Am I barking up the wrong tree, or is there a simpler way to do it?

Your advice would be appreciated.

Many thanks

11:13 pm on Jan 14, 2016 (gmt 0)

New User

joined:Jan 14, 2016
posts: 3
votes: 0

I believe I may now be making some progress, but I may be going off at a tangent.

I have added the following which now outputs the previous and next months:

$nextmonth = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));

$nextmonth = date('F Y', strtotime('+1 month'));

echo $nextmonth. "<br>";

$lastmonth = date('F Y', strtotime('-1 month'));

echo $lastmonth;

I presume that I must now use the two variables in a form button element?

11:55 pm on Jan 14, 2016 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 11, 2015
posts: 328
votes: 24

You could allow your script to accept a date (or just month/year?) as a URL parameter. If no date is supplied then default to the current month. This would allow you to link to (or even bookmark) specific months. Your "next" and "previous" month buttons could then simply be links to the current script with the pre-calculated $nextmonth and $lastmonth dates as URL parameters (in the appropriate format).

For example:

<a href="mycalendar.php?date=2015-12-14">Previous Month</a>
<a href="mycalendar.php?date=2016-02-14">Next Month</a>
12:09 pm on Jan 20, 2016 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
votes: 0

The other thing you can do is write the 'current' month into a session variable; and then your links are fixed (e.g. ?go=1 / ?go=-1 )

if (is_set($_SESSION['date']) $date=$_SESSION['date'];
else $date=time();
$nextmonth = mktime(0, 0, 0, date("m",$date)+1, date("d",$date), date("Y",$date));
$lastmonth = mktime(0, 0, 0, date("m",$date)-1, date("d",$date), date("Y",$date));
if ($_GET['go']==1) $date=$nextmonth;
if ($_GET['go']==-1) $date=$lastmonth;
//after processing etc...
//now use $date for your output...
3:52 pm on Jan 21, 2016 (gmt 0)

New User

joined:Jan 14, 2016
posts: 3
votes: 0

Hi and thanks for the replies.
Sorry for the slow response but it has been a difficult week to say the least.

I hope to get back to this project within a couple of days and I will try and work through your solutions.

Many thanks once again.