user3129227
user3129227

Reputation: 51

Get Next Month In PHP

I have a php calendar that works fine for THIS month but I'd like to create another 2 scripts that show the next 2 months. I want to use the same format as the one I already have but I'm not completely sure on what I need to change to get it to display the dates of months 2 & 3. Here is my code for Month 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);
?>

Can anyone help me with this?

Upvotes: 0

Views: 5827

Answers (4)

Christopher Thomas
Christopher Thomas

Reputation: 4702

The most compact answer I can think of to this problem, which is surprising considering it's so common and yet it has problems to use the +1 month type answers, which I see skipping over 28 day februarys etc, etc. Is as follows:

// starting values
$month = 1;
$year = 2020;

// Increment the month to the next one
$month = $month + 1;

// If the month is greater than 12, add a year
$year = $month > 12 ? $year+1 : $year;

// Afterwards use the modulo of the month against 12
// but if it equals 0, use ?: to override that to 1
$month = $month % 12 ?: 1;

So obviously this can be cut down to three uncommented lines. So its really short and to the point. I've tried it with numerous iterations and starting places and I've not found a problem with it yet.

I wrote a small program which tests it:

<?php

$year=2020;
$month=0;
$a = 0;

do{
    $month = $month + 1;
    $year = $month > 12 ? $year+1 : $year;
    $month = $month % 12 ?: 12;
    $f="$year-$month";
    $d = new \DateTime(date($f));
    print("$f == Y:".$d->format('Y').", m:".$d->format('F')."\n");

    $a++;
}while($a<64);

and it outputs this:

2020-1 == Y:2020, m:January
2020-2 == Y:2020, m:February
2020-3 == Y:2020, m:March
2020-4 == Y:2020, m:April
2020-5 == Y:2020, m:May
2020-6 == Y:2020, m:June
2020-7 == Y:2020, m:July
2020-8 == Y:2020, m:August
2020-9 == Y:2020, m:September
2020-10 == Y:2020, m:October
2020-11 == Y:2020, m:November
2020-12 == Y:2020, m:December
2021-1 == Y:2021, m:January
2021-2 == Y:2021, m:February
2021-3 == Y:2021, m:March
2021-4 == Y:2021, m:April
2021-5 == Y:2021, m:May
2021-6 == Y:2021, m:June
2021-7 == Y:2021, m:July
2021-8 == Y:2021, m:August
2021-9 == Y:2021, m:September
2021-10 == Y:2021, m:October
2021-11 == Y:2021, m:November
2021-12 == Y:2021, m:December
2022-1 == Y:2022, m:January
2022-2 == Y:2022, m:February
2022-3 == Y:2022, m:March
2022-4 == Y:2022, m:April
2022-5 == Y:2022, m:May
2022-6 == Y:2022, m:June
2022-7 == Y:2022, m:July
2022-8 == Y:2022, m:August
2022-9 == Y:2022, m:September
2022-10 == Y:2022, m:October
2022-11 == Y:2022, m:November
2022-12 == Y:2022, m:December
2023-1 == Y:2023, m:January
2023-2 == Y:2023, m:February

Upvotes: 0

Clic
Clic

Reputation: 11

<?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 />");
}
?>

Will output :

December 2013
January 2014
February 2014

Assuming that we are in december, 2013.

Upvotes: 1

Tarar
Tarar

Reputation: 38

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

strtotime returns timestamp for next and date('F') will convert it to your desired format.

Upvotes: 2

Rajasekhar
Rajasekhar

Reputation: 527

Try This :

$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

Upvotes: 2

Related Questions