justinh Posted October 31, 2008 Share Posted October 31, 2008 I'm trying to make my calender able to cycle through the months. But for some reason my function doesn't update the calender.. but it does work. you can see the calender here.http://www.wmptest.com/HuntReserver/calender.php here is the code in question i have: <?php function NavigateCalender($month){ if (isset($_GET['direction'])){ switch($_GET['direction']){ case "1": $month--; echo $month; break; case "2": $month++; echo $month; break; } } else { } } $date = time(); $day = date('d',$date); $month = date('m',$date); NavigateCalender($month); $year = date('y',$date); $firstday = mktime(0,0,0, $month, 1, $year); $title = date('F', $firstday); $dayofweek = date('D', $firstday); switch($dayofweek){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } $daysinmonth = cal_days_in_month(0, $month, $year); echo "<table border=1 width=294>"; echo "<tr><th colspan=7><a href=\"calender.php?direction=1\"><</a> $month - $year <a href=\"calender.php?direction=2\">></a></th></tr>"; echo"<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td> <td width=42>S</td></tr>"; Thanks abunch! Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/ Share on other sites More sharing options...
envexlabs Posted October 31, 2008 Share Posted October 31, 2008 maybe for the switch try $month + 1, or $month - 1 instead of -- and ++ Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679334 Share on other sites More sharing options...
justinh Posted October 31, 2008 Author Share Posted October 31, 2008 i tried your suggestion but it still isnt working. http://www.wmptest.com/HuntReserver/calender.php <?php function NavigateCalender($month){ if (isset($_GET['direction'])){ switch($_GET['direction']){ case "1": $month - 1; echo $month; break; case "2": $month + 1; echo $month; break; } } else { } } $date = time(); $day = date('d',$date); $month = date('m',$date); NavigateCalender($month); $year = date('y',$date); $firstday = mktime(0,0,0, $month, 1, $year); $title = date('F', $firstday); $dayofweek = date('D', $firstday); switch($dayofweek){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } $daysinmonth = cal_days_in_month(0, $month, $year); echo "<table border=1 width=294>"; echo "<tr><th colspan=7><a href=\"calender.php?direction=1\"><</a> $month - $year <a href=\"calender.php?direction=2\">></a></th></tr>"; echo"<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td> <td width=42>S</td></tr>"; Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679350 Share on other sites More sharing options...
justinh Posted October 31, 2008 Author Share Posted October 31, 2008 Is this impossible to do in PHP? Or am I just wording my question wrong? It seem's like no forums have a suggestion for me. Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679377 Share on other sites More sharing options...
justinh Posted October 31, 2008 Author Share Posted October 31, 2008 could AJAX solve my problem? ??? Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679396 Share on other sites More sharing options...
kenrbnsn Posted October 31, 2008 Share Posted October 31, 2008 I took a look at your code. You had a few problems. 1) your function NavigateCalender() never returned the new month 2) You need to pass the month that's current being displayed on the URL so the script knows which month to start from 3) the function NavigateCalender() really needs to figure out and return both the month and year. Here's my take on your code (it does seem to work): <?php function NavigateCalender($month, $year){ $cur = strtotime($year . '-' . $month . '-01'); if (isset($_GET['direction'])){ switch($_GET['direction']){ case "1": list($year,$month) = explode('-',date('Y-m',strtotime('-1 month',$cur))); break; case "2": list($year,$month) = explode('-',date('Y-m',strtotime('+1 month',$cur))); break; } } return(array($month,$year)); } $date = time(); $day = date('d',$date); $month = (isset($_GET['month']))?$_GET['month']:date('m',$date); $year = (isset($_GET['year']))?$_GET['year']:date('Y',$date); list($month,$year) = NavigateCalender($month,$year); $firstday = mktime(0,0,0, $month, 1, $year); $title = date('F', $firstday); $dayofweek = date('D', $firstday); switch($dayofweek){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } $daysinmonth = date('t',strtotime($year . '-' . $month . '-01')); echo "<table border=1 width=294>"; echo '<tr><th colspan=7><a href="?direction=1&month=' . $month . '&year=' . $year . '">< </a>' . $month . ' - '. $year . '<a href="?direction=2&month=' . $month . '&year=' . $year . '"> ></a></th></tr>'; echo"<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td> <td width=42>S</td></tr>"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679511 Share on other sites More sharing options...
justinh Posted October 31, 2008 Author Share Posted October 31, 2008 i <3 u ken Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679646 Share on other sites More sharing options...
justinh Posted October 31, 2008 Author Share Posted October 31, 2008 don't know if your still here Ken but i was wondering if you could explain this line you added: $cur = strtotime($year . '-' . $month . '-01'); I'm trying to understand how the code you gave me works. Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679676 Share on other sites More sharing options...
kenrbnsn Posted November 1, 2008 Share Posted November 1, 2008 The function strtotime() takes two parameters. The first is the date/time string to be converted. The second, which most people don't use, is the starting timestamp that the first parameter uses. So that statement gets the time stamp for the 1st day of the year/month that's passed to the function and uses that to go forward or back one month. You can shorten the function a little by doing: <?php function NavigateCalender($month, $year){ $cur = strtotime($year . '-' . $month . '-01'); if (isset($_GET['direction'])){ $diff = ($_GET['direction'] == 1)?'-':'+'; list($year,$month) = explode('-',date('Y-m',strtotime($diff . '1 month',$cur))); } return(array($month,$year)); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/130878-php-issue/#findComment-679951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.