2014-12-31 83 views
0

与我的代码,我试图使URL到上一个或下一个月的一个按钮点击根据当前的网址。PHP mktime不按预期工作

例如,如果我将URL的末尾设为index?view=list&month=January&year=2014,那么我希望上一个按钮去index?view=list&month=December&year=2013。对于1月,它工作正常,但当月份是2月份时,上一个按钮等于2月份,下一个按钮是4月份。

上一个按钮

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'])); ?> 

下一个按钮

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'])); ?> 
+0

你需要翻身的一年以及? –

+0

日期('F',13)===“December” –

+0

是的,如果是2014年1月,它应该会翻转到2013年12月 –

回答

4

此代码可能工作更顺畅适合你。它使用的的strtotime的前进日期能力或向后:

上一页按钮

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

下一步按钮

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')); ?> 
+0

你甚至不需要内部时间的'1' )只要使用“月份年”就足够了。 –

+0

而实际上你甚至不需要将它们嵌套在一起。它应该像'strtotime(“2014年3月+1个月”)''一样使用单一表达式。 –

+0

感谢您的提示!我编辑了代码。 –