Forum Moderators: coopster

Message Too Old, No Replies

getdate this and next month

How to easily display this and next month with getdate or similar

         

broniusm

6:15 am on Jan 10, 2004 (gmt 0)

10+ Year Member



Happy new year!

Using getdate(), I can easily retrieve "January" for this month. What's an easy way to get next month as well?

twist

7:01 am on Jan 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will give you the current and next month. Hope it helps.


$month = getdate();
$month = $month['mon'];
$next = $month + 1;
$month = date( "F", mktime(0, 0, 0, $month, 1, 0 ) );
$next = date( "F", mktime(0, 0, 0, $next, 1, 0 ) );
echo 'Current month is '. $month .' and next month is '. $next .'<br>';

dcrombie

2:42 pm on Jan 10, 2004 (gmt 0)



$month = date ("F");
$next = date ("F", strtotime ("+1 month"));

I first tried "next month" but that was skipping a month... not sure why?!?

;)

coopster

4:04 pm on Jan 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>I first tried "next month" but that was skipping a month... not sure why?!?

From the GNU Date input formats:

A few numbers may be written out in words in most contexts. This is most useful for specifying day of the week items or relative items (see below). Here is the list:
'first'
for 1,
'next'
for 2,
'third'
for 3,
'fourth'
for 4,
'fifth'
for 5,
'sixth'
for 6,
'seventh'
for 7,
'eighth'
for 8,
'ninth'
for 9,
'tenth'
for 10,
'eleventh'
for 11 and
'twelfth'
for 12. Also,
'last'
means exactly -1.

I don't claim to be an expert on the GNU Date input formats [gnu.org], but I believe the way

first, next, third,
etc. operates might be translated as follows:

The

first month
after this month. (Adds 1)
The
next month
after the first month after the current month. (Adds 2)
The
third month
after that. (Adds 3)
...
The last month. (Subtracts 1)

Testing I have performed proved the calculations true, I'm just not sure my translation is what the programmers would agree on. But, heh, it works for me ;)



So, the reason you are "skipping" a month when using
next month
is that the function is adding 2 to the current month. Your code would work fine if you stated you wanted to show the
first month
after the current month, dcrombie:

$month = date("F");
$next = date("F", strtotime("first month"));

broniusm

4:38 pm on Jan 12, 2004 (gmt 0)

10+ Year Member



Thanks all, and I especially appreciate the incidental ensuing conversation. I ended up using a hybdrid of the suggestions, as is usually the case, and it works like a dream.

g1smd

11:30 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In six weeks time, there is a leap day on 2004-02-29.

Is your code robust enough for that?