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
https://forums.phpfreaks.com/topic/201211-next-month/
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
https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055640
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
https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055932
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
https://forums.phpfreaks.com/topic/201211-next-month/#findComment-1055939
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.