bschultz Posted May 10, 2010 Share Posted May 10, 2010 I'm working on adding a "last month" and a "next month" link to a calendar. Here's what I have... <?php $thismonth = date('m'); $thisyear = date('Y'); $last_month = date("m", strtotime("-1 months")); $next_month = date("m", strtotime("+1 months")); echo "<a href='month.php?thismonth=$last_month&thisyear=2010'>Previous Month <<<<<<</a>"; ?> How can I get the year part to change based on the month that's being displayed? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted May 10, 2010 Share Posted May 10, 2010 date("m-Y", strtotime("-1 months")) HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
bschultz Posted May 10, 2010 Author Share Posted May 10, 2010 How do I work that into the link though? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 10, 2010 Share Posted May 10, 2010 I assume you want something like "$last_year" to go with "$last_month", so <?php $last_month = date('m',strtotime("-1 month")); $last_year = date('Y',strtotime("-1 month")); echo "<a href='month.php?thismonth=$last_month&thisyear=$last_year'>Previous Month <<<<<<</a>"; ?> Ken Quote Link to comment Share on other sites More sharing options...
bschultz Posted May 10, 2010 Author Share Posted May 10, 2010 Great...that worked. Now, once I get to that link, the month and year variables will be passed in a $_GET fashion. I've tried this: <?php $thismonth = $_GET['thismonth']; $thisyear = $_GET['thisyear']; $last_month = date('m', strtotime('-1 month', strtotime($thismonth)); $last_year = date('Y', strtotime('-1 month', strtotime($thismonth)); $next_month = date('m', strtotime('+1 month', strtotime($thismonth)); $next_year = date('Y', strtotime('+1 month', strtotime($thismonth)); ?> This gives me a Parse error: syntax error, unexpected ';' $_GET['thismonth'] will give an integer (May is "05")...is that what's throwing this off? How would I fix this? Thanks! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 10, 2010 Share Posted May 10, 2010 Look at each of these lines: <?php $last_month = date('m', strtotime('-1 month', strtotime($thismonth)); $last_year = date('Y', strtotime('-1 month', strtotime($thismonth)); $next_month = date('m', strtotime('+1 month', strtotime($thismonth)); $next_year = date('Y', strtotime('+1 month', strtotime($thismonth)); ?> There are three opening "(" but only two closing ")". You're missing one ")" on each line. For this to work, you need to include the year, month, & day in the second strtotime(), so do something like: <?php $testdate = $_GET['thisyear'] . '-' . $_GET['thismonth'] . '-01'; $last_month = date('m', strtotime('-1 month', strtotime($testdate))); $last_year = date('Y', strtotime('-1 month', strtotime($testdate))); $next_month = date('m', strtotime('+1 month', strtotime($testdate))); $next_year = date('Y', strtotime('+1 month', strtotime($testdate))); ?> Ken Quote Link to comment Share on other sites More sharing options...
bschultz Posted May 10, 2010 Author Share Posted May 10, 2010 Man, I hate dates in PHP! Worked great...thanks Ken! Quote Link to comment 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.