ngreenwood6 Posted October 16, 2008 Share Posted October 16, 2008 I have the following code: //get the current month from the url $month = $_GET['mon']; //make the month into words $real_month = getdate(mktime(0,0,0,$month,$date['mday'],$date['year'])); //get the current month $date = getdate(); //get the previous month $date_previous_month = getdate(mktime(0,0,0,$month-1,$date['mday'],$date['year'])); $previous_month = $date_previous_month['month']; //get the next month $date_next_month = getdate(mktime(0,0,0,$month+1,$date['mday'],$date['year'])); $next_month = $date_next_month['month']; ?> <div id="calendar"> <center> <table> <th colspan="7"> <a href="calendar.php?mon=<?php echo $date_previous_month['mon']; ?>"><<</a> <?php echo $real_month['month']." ".$date['year'];; ?> <a href="calendar.php?mon=<?php echo $date_next_month['mon']; ?>">>></a> </th> </table> </center> </div> There is more to it but that is all that is in use. Now in the url it passes the current month in a number format when you reach this page. At the current time It is showing the correct month and year and goes back and forth. The only problem is that when you go past december it does not change to the next year or when you go to before january it doesnt go back a year. I am just trying to create a simple calendar just for testing purposes. If anyone can help it would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/ Share on other sites More sharing options...
ngreenwood6 Posted October 16, 2008 Author Share Posted October 16, 2008 My thinking was something like this: if($date_next_month = 1) { $date_next_month = getdate(mktime(0,0,0,$month+1,$date['mday'],$date['year']+1)); } The only problem with that is that it only changes it for that one month. Any ideas. Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667388 Share on other sites More sharing options...
discomatt Posted October 16, 2008 Share Posted October 16, 2008 You're better off passing both the year and the month in the query string... you can then do something like this if( $_GET['m'] == 1 ) { $prevMonth = 12; $prevYear = $_GET['y'] - 1; } elseif( $_GET['m'] == 12 ) { $nextMonth = 1; $nextYear = $_GET['y'] + 1; } Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667391 Share on other sites More sharing options...
ngreenwood6 Posted October 16, 2008 Author Share Posted October 16, 2008 Ok I will give that a shot but any other suggestions without passing the year through the url? Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667501 Share on other sites More sharing options...
ngreenwood6 Posted October 16, 2008 Author Share Posted October 16, 2008 I tried your suggestion but it did not work. It did the same thing that mine did. It changed the year once and then the next month it went right back. Any other suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667561 Share on other sites More sharing options...
Barand Posted October 16, 2008 Share Posted October 16, 2008 try <?php $month = isset($_GET['m']) ? $_GET['m'] : date('n'); $year = isset($_GET['y']) ? $_GET['y'] : date('Y'); $prevm = $month==1 ? 12 : $month-1; $prevy = $month==1 ? $year-1 : $year; $nextm = $month==12 ? 1 : $month+1; $nexty = $month==12 ? $year+1 : $year; echo "<a href='?m=$prevm&y=$prevy'><<</a> "; echo date('M Y', mktime(0,0,0,$month,1,$year)); echo " <a href='?m=$nextm&y=$nexty'>>></a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667572 Share on other sites More sharing options...
ngreenwood6 Posted October 16, 2008 Author Share Posted October 16, 2008 I will give that a shot and let you know. Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667576 Share on other sites More sharing options...
kenrbnsn Posted October 17, 2008 Share Posted October 17, 2008 Instead of fooling with manual date calculations, let PHP do them for you: <?php //get the current month from the url $month = isset($_GET['mon'])?$_GET['mon']:date('m'); $year = isset($_GET['yr'])?$_GET['yr']:date('Y'); $passed_my = strtotime($year . '-' . $month . '-01'); //get the previous month $date_previous_month = date('m',strtotime('-1 month',$passed_my)); $date_next_month = date('m',strtotime('+1 month',$passed_my)); $date_previous_year = date('Y',strtotime('-1 month',$passed_my)); $date_next_year = date('Y',strtotime('+1 month',$passed_my)); ?> <table> <th colspan="7"> <a href="?mon=<?php echo $date_previous_month; ?>&yr=<?php echo $date_previous_year; ?>"><<</a> <?php echo date('F Y',$passed_my); ?> <a href="?mon=<?php echo $date_next_month; ?>&yr=<?php echo $date_next_year; ?>">>></a> </th> </table> Ken Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667614 Share on other sites More sharing options...
ngreenwood6 Posted October 17, 2008 Author Share Posted October 17, 2008 Thanks that is what I was looking for kenrbnsn. I will try to work with that and add onto it. Thanks for the help everyone. Quote Link to comment https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-668312 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.