Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/191502-help-with-nextprevous-month-year/
Share on other sites

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]

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

<?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;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.