views:

35

answers:

2

Hi Everyone,

I'm building my first application that requires a calendar. I have it working great for the current month. My problem is that i'm not sure how to make it so that when a user clicks the link to go to the next month, it will show the next month.

I'll try to only post the code that is necessary so here's my variables

//gets todays date
$date = time();

//This puts the day, month, and year in separate variables
$day = date('d', $date);
$month = date('m', $date);
$year = date('y', $date);
$bigYear = date('Y', $date);
$first_day = mktime(0, 0, 0, $month, 1, $year);             //we need to generate the first day of the month
$month_name = date('F', $first_day);                        //get the current month's full spelling
$day_of_week = date('D', $first_day);                       //find out what day of the week the first day of the month falls on
$prevM = getDate(mktime(0, 0, 0, $month-1, 1, $year));      //gets an associative array of the previous month
$nextM = getDate(mktime(0, 0, 0, $month+1, 1, $year));      //gets an associative array of the next month
$prevMonth = $prevM['month'];                               //gets the actual previous month's name
$nextMonth = $nextM['month'];                               //gets the actual next month's name
$day_count = 1;                                             //counts the days up to 7 so we know when to start a new week
$day_num = 1;                                               //counter for the total number of days in the month

and here's my links that are supposed to take you to the next and previous months

echo "<th colspan=2 class=\"noBorder\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevMonth'>&laquo;</a></th>";
echo "<th colspan=3 class=\"noBorder\"><strong>$month_name</strong></th>";
echo "<th colspan=2 class=\"noBorder\"><a href='{$_SERVER['PHP_SELF']}?currentpage=$nextMonth'>&raquo;</a></th>";
+1  A: 

Something like:

$date = empty($_GET['currentpage']) ? time() : $_GET['currentpage'];
MattSmith
If i do that and then click one of the links, it always sets the $month_name to December. Do you know why that is?
Catfish
just needed to change it to `$date = empty($_GET['currentpage']) ? time() : strtotime($_GET['currentpage']);`
Catfish
This doesn't work because, after December 2010 you will jump back to January 2010 (not January 2011!). You also have to pass the year.
captaintokyo
+1  A: 

Try this:

$date = empty($_GET['currentpage']) ? time() : strtotime($_GET['currentpage']);

And:

$prevMonth = "{$prevM['year']}-{$prevM['mon']}";
$nextMonth = "{$nextM['year']}-{$nextM['mon']}";
captaintokyo
Thanks for this! I was just testing it and realized i was stuck in the current year and was about to ask for this. Much appreciated!
Catfish