Reputation: 6625
With my code I'm trying to make the URL go to the previous or next month on a button click based on what is in the current URL.
For example if I have the end of the URL as index?view=list&month=January&year=2014
then I would want the previous button to go to index?view=list&month=December&year=2013
. For January it works fine, but when the month is February the previous button equals February and the next button is April.
Previous Button
onclick="location.href='?view=list
&month=<?php echo date("F", mktime(0, 0, 0, date('n', strtotime($_GET['month'])) - 1, 1, $_GET['year'])); ?>
&year=<?php echo date("Y", mktime(0, 0, 0, date('n', strtotime($_GET['month'])) - 1, 1, $_GET['year'])); ?>
Next Button
onclick="location.href='?view=list
&month=<?php echo date("F", mktime(0, 0, 0, date('n', strtotime($_GET['month'])) + 1, 1, $_GET['year'])); ?>
&year=<?php echo date("Y", mktime(0, 0, 0, date('n', strtotime($_GET['month'])) + 1, 1, $_GET['year'])); ?>
Upvotes: 0
Views: 178
Reputation: 2933
This code may work smoother for you. It makes use of the strtotime's ability to move the date forward or backward:
Previous button:
onclick="location.href='?view=list
&month=<?php echo date('F', strtotime($_GET['month'].' '.$_GET['year'].' -1 month')); ?>
&year=<?php echo date('Y', strtotime($_GET['month'].' '.$_GET['year'].' -1 month')); ?>
Next button:
onclick="location.href='?view=list
&month=<?php echo date('F', strtotime($_GET['month'].' '.$_GET['year'].' +1 month')); ?>
&year=<?php echo date('Y', strtotime($_GET['month'].' '.$_GET['year'].' +1 month')); ?>
Upvotes: 4