Mr Chris Posted February 9, 2010 Share Posted February 9, 2010 Hi I'm hoping someone can advise me please. Say I have a month and a year set to variables (no day) called via $_GET from a URL string /?year=2008&month=12 $year = $_GET['year']; // 2008 $month = $_GET['month']; // EG (12) So = December How can I write a html link dynamically depending on the values of $_GET which goes back a month and also goes forward a month with years also changing depending on the month of the year? So for example on my page my link for next month (in this instance) would be: <a href=2009/01>Next Month</a> and the previous month <a href=2008/11>Previous Month</a> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/191502-help-with-nextprevous-month-year/ Share on other sites More sharing options...
mikesta707 Posted February 9, 2010 Share Posted February 9, 2010 last month and next month echo $nextMonth = date("F", strtotime("last month")); echo "<br />"; echo $prevMonth = date("F", strtotime("next month")); outputs: January March if you want years, just do echo $nextMonth = date("Y", strtotime("last year")); echo "<br />"; echo $prevMonth = date("Y", strtotime("next year")); which outputs 2009 2011 check out the manual page for the date for different formats of the date, and strtotime for more info on strtotime function[/code] Quote Link to comment https://forums.phpfreaks.com/topic/191502-help-with-nextprevous-month-year/#findComment-1009501 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 $lastmonth = strftime("%Y/%m",strtotime(' -1 month'); $nextmonth = strftime("%Y/%m",strtotime(' +1 month'); Quote Link to comment https://forums.phpfreaks.com/topic/191502-help-with-nextprevous-month-year/#findComment-1009502 Share on other sites More sharing options...
Mr Chris Posted February 10, 2010 Author Share Posted February 10, 2010 Thanks Guys, One problem with that though the next month and year need to be created through variables with values assigned to then, which is where I was getting it wrong e.g: <?php $the_month = 09; //Set as August $the_year = 1999; //Set as 1999 ?> So I was hoping to do something like this: <?php $the_month = 09; //Set as August $the_year = 1999; //Set as 1999 echo $lastMonth = date("".$the_month."", strtotime("-1 month")); //Would be 08 echo "<br />"; echo $nextMonth = date("".$the_month."", strtotime("+1 month")); //Would be 10 echo "<br />"; echo $lastYear = date("".$the_year."", strtotime("-1 year")); //Would be 1998 echo "<br />"; echo $nextYear = date("".$the_year."", strtotime("+1 year")); //Would be 2000 ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/191502-help-with-nextprevous-month-year/#findComment-1009940 Share on other sites More sharing options...
sasa Posted February 10, 2010 Share Posted February 10, 2010 <?php $the_month = '12'; //Set as August $the_year = '1999'; //Set as 1999 $temp = $the_month+1; $the_next_month = str_pad($temp>12 ? $temp-12 : $temp,2,'0',0); $the_next_year = $temp>12 ? $the_year+1 : $the_year; echo $the_next_month,'-',$the_next_year; ?> Quote Link to comment https://forums.phpfreaks.com/topic/191502-help-with-nextprevous-month-year/#findComment-1010011 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.