Jump to content

Next Month


bschultz

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.