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? Link to comment https://forums.phpfreaks.com/topic/201211-next-month/ 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 Link to comment https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055636 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? Link to comment https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055638 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 Link to comment https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055640 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! Link to comment https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055932 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 Link to comment https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055939 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! Link to comment https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055941 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.