2010-09-29 60 views
2

我正在构建需要日历的第一个应用程序。我在当前的月份工作很好。我的问题是,我不知道如何做到这一点,以便当用户点击链接去下个月,它会显示下个月。PHP日历 - 除当前月份以外的任何月份出现问题

我会尽量只张贴是必要的,这里是我的变量

//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 

的代码,这里是我的,都应该链接带你到下一个和前几个月

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

试试这个:

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

和:

$prevMonth = "{$prevM['year']}-{$prevM['mon']}"; 
$nextMonth = "{$nextM['year']}-{$nextM['mon']}"; 
+0

感谢您的支持!我只是测试它,并意识到我被困在当年,并且即将要求这个。非常感激! – Catfish 2010-09-29 04:28:25

1

喜欢的东西:

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

如果我这样做,然后点击其中一个链接,它总是把$ MONTH_NAME到12月。你知道这是为什么吗? – Catfish 2010-09-29 04:09:38

+0

只需要将其更改为'$ date = empty($ _ GET ['currentpage'])? time():strtotime($ _ GET ['currentpage']);' – Catfish 2010-09-29 04:22:26

+0

这不起作用,因为2010年12月之后,你将跳回到2010年1月(不是2011年1月!)。你也必须通过一年。 – Mischa 2010-09-29 04:27:12