用PHP获取下个月

时间:2013-12-24 10:16:36

标签: php calendar

我有一个PHP日历在本月工作正常,但我想创建另外两个显示未来2个月的脚本。我想使用与我已有的格式相同的格式,但我不完全确定我需要更改以显示第2个月和第2个月的日期。 3.这是第1个月的代码:

<?php 
    $currDay        =    date("j");
    $today          =    date("d"); // Current day
    $month          =    date("m"); // Current month
    $displaymonth   =    date("F");
    $year           =    date("Y"); // Current year

    // Days in current month
    $days           =    cal_days_in_month(CAL_GREGORIAN,$month,$year);
?>

任何人都可以帮我吗?

3 个答案:

答案 0 :(得分:3)

试试这个:

$datetime = new DateTime(date('Y-m-d')); // Date object for current date
$datetime->modify('next month'); // Modifying object to next month date
$next_month_num = $datetime->format('m'); // In Numeric 01,02...
$next_month_str = $datetime->format('M'); // In String Jan,Feb

答案 1 :(得分:1)

$firstMonth= date('F', strtotime('+1 months'));
$secondMonth= date('F', strtotime('+1 months'));

strtotime返回下一个时间戳,而日期('F')会将其转换为您想要的格式。

答案 2 :(得分:1)

<?php
foreach(range(0,2) as $monthoffset)
{  $datetime = new DateTime(date('Y-m-d')); // Date object for current date
    $datetime->modify('+'.$monthoffset.' month'); // Modifying object
    $timestamp=$datetime->getTimestamp(); // Getting the resulting timestamp, required by date function

    // Setting the constants your calendar 
    $currDay        =    date("j",$timestamp);
    $today          =    date("d",$timestamp); // Day
    $month          =    date("m",$timestamp); // Month
    $displaymonth   =    date("F",$timestamp);
    $year           =    date("Y",$timestamp); // Year

    // Days current month
    $days           =    cal_days_in_month(CAL_GREGORIAN,$month,$year);

    // Put the code which displays your calendar from here. It will be executed three times
    echo($displaymonth." ".$year."<br />");
}
?>

将输出:

December 2013
January 2014
February 2014

假设我们在2013年12月。