日期Ym加+1个月


date Ym adding +1 month

我需要添加/substract+1/-1个月才能进行动态导航。到目前为止,我得到的是——仍然在为日期问题而挣扎:events.html?月份=201401

<?php
$getdate=stripslashes((isset($_GET['month'])) ? $_GET ['month'] : "");
?>
<a href="events.html?month=<?php if($getdate=="")echo date('Ym',strtotime(date('Y-m-d').' +1 month')); else echo date('Ym',strtotime($getdate.' +1 month')); ?>">month +</a>

这应该是这样的结尾:

<november    actual    january>

有人能帮忙吗?仍然获得了201401作为href,这对于第一次点击是正确的。但它在第二次点击时停留在201401,依此类推。

这应该有效:

$month = isset($_GET['month']) ? $_GET['month'] : date('Ym');
$curr = DateTime::createFromFormat('!Ym', $month);
$prev = clone $curr; $prev->modify('-1 month');
$next = clone $curr; $next->modify('+1 month');
echo "PREV: " . $prev->format('F Y') . "'n";
echo "CURR: " . $curr->format('F Y') . "'n";
echo "NEXT: " . $next->format('F Y') . "'n";

DEMO

这样就可以了。

<?php
$date = new DateTime('2000-01-01'); // you can add you date here coming via $_GET['month']
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d') . "'n";
?>

参考编号:http://www.php.net/manual/en/datetime.add.php

已修复:

$currentDate = !empty($_GET['month']) ? $_GET['month'] : date('Ym');
$currentTimespan = mktime(0, 0, 0, substr($currentDate, 4, 2), 1, substr($currentDate, 0, 4));
$actual = date('Ym', $currentTimespan);
$previous = date('Ym', strtotime('-1 month', $currentTimespan));
$next = date('Ym', strtotime('+1 month', $currentTimespan));

您的问题是strtotime()函数无法识别您使用的格式(Ym),因此您必须使用ATOM格式("Y-m-d''TH:i:sP")子字符串或其他规范化格式。;)

<?php
$getdate = (strlen($_GET['month'])>0)?$_GET['month']:date('Y-m');
$month = date("Y-m", strtotime("+1 month", strtotime($getdate)));
echo '<a href="sof_answer.php?month='.$month.'">month +</a>';
?>

非常简单。添加一个月,然后只获取该月:

$time = new DateTime(); 
$time->modify('+1 month');
$month = $time->format('m'); 
print_r ($month);