2

trying to echo 3 months into my WordPress website according to the todays month. 1) This month 2) Next month 3) Third month.

I have an array with names of the months:

$months = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
];

To print out current month:

<?php echo $months[(int)date('m')-1]; ?>

To print out next month:

<?php echo $months[((int)date('m') == 12 ? 1 : (int)date('m') + 1)-1]; ?>

Both if these work just fine. But when I try to print out third month instead of January I get February. Could you help me figure out why?

<?php echo $months[((int)date('m') == 11 ? 1 : (int)date('m') == 12 ? 2 : (int)date('m') + 2)-1]; ?>

I get the answer 0, so it should be January, but seems that

(int)date('m') == 12 ? 2

part is executed, instead of the first one.

Forrar
  • 35
  • 6

3 Answers3

1

Likely because your ternary operator is evaluating incorrectly.
You also don't need to cast to integer because php does that for you.

echo $months[(date('m') == 11 ? 1 : (date('m') == 12 ? 2 : date('m') + 2))-1];

Outputs: January.

See the change I made? I added () around date('m') == 12 ? 2 : date('m') + 2

IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25
  • 1
    PHP's evaluation of ternary operators also differs from other languages, which parentheses take care of, [see Wikipedia](https://en.wikipedia.org/wiki/%3F:#PHP). – janh Nov 03 '17 at 09:30
  • Thank you, adding () solved the problem! Though still not sure why they are needed. – Forrar Nov 03 '17 at 10:03
  • Because without the brackets, it's `$months[((date('m') == 11 ? 1: date('m') == 12) ? 2 : date('m') +2 ) -1);` so it always returned "2" because `date('m') == 11 ? 1: date('m') == 12` returns true – IsThisJavascript Nov 03 '17 at 10:26
1

The easiest way is to do this using PHP built-in date functions, I mean why reinventing the wheel?

$now = date('Y-m-1');
echo date('m');
echo date('m', strtotime($now . ' + 1 month'));
echo date('m', strtotime($now. ' + 2 month'));
teeyo
  • 3,665
  • 3
  • 22
  • 37
  • He's not reinventing the wheel. His example says; if today is November, print out January, however if today is December print out February. God knows what for though! – IsThisJavascript Nov 03 '17 at 09:42
  • Well, that's what I'm saying why creating a months array in the first place if you can do this with built-in functions, that's reinventing for me! – teeyo Nov 03 '17 at 09:47
  • 1
    It's for an event calendar where you can choose to display calendar of the other month. The solution provided by you seems super easy, is there any way to make the names of the month to appear in other language, not English? Sorry, total beginner with programming. – Forrar Nov 03 '17 at 10:04
  • 1
    Yes, there's a way :) check this answer here https://stackoverflow.com/questions/4975854/translating-php-date-for-multilingual-site – teeyo Nov 03 '17 at 10:09
1

You might want to check out the strtotime function in combination with the date function:

<?php
echo date("F", strtotime("first day of +1 month"));

If you want to localize the month-names you could change the first date-parameter to n which gives you the numeric representation of the month and could then be a key to your array.

This might be an easier and more readable solution to your problem.

afk
  • 123
  • 7
  • 1
    This will lead to problems, e.g. it'll print March on January 30st. Use "first day of +1 month", which basically means "+1 month, based on the first day of this month". – janh Nov 03 '17 at 09:47
  • Thanks @janh for pointing out this "edge case" ;-) Updated the answer. – afk Nov 03 '17 at 09:56