-5

I think the title is clear! I want to make to take a number between 1-12 and give back the name of the month in that number (and if possible I want to have custom names for months).

  • 1
    a simple array containing the (custom) names should do the trick. `$months = ["default", "Jänner", "Febrario",..];` – Jeff Aug 24 '18 at 07:44
  • 4
    Possible duplicate of [Convert number to month name in PHP](https://stackoverflow.com/questions/18467669/convert-number-to-month-name-in-php) – apokryfos Aug 24 '18 at 07:47
  • @apokryfos not sure that is a good duplicate. It's not until 75% down the page you get to an answer which gives the option of custom month names as OP request. All the other are "date()" conversions of some kind – Andreas Aug 24 '18 at 07:59
  • @Andreas I've chosen to ignore the "custom names for months" part mostly, because it does feel like an XY problem. I don't think it actually makes sense to ask for the name of month 5 and get something like "strawberry" back. If the question would have been about internationalisation then that's a different question completely. – apokryfos Aug 24 '18 at 08:04
  • @apokryfos I believe it's due to OP wanting it in a different language. Not all languages are supported in PHP as far as I know. – Andreas Aug 24 '18 at 08:10
  • @Andreas as I said, that's a different question completely. Unless OP wants a "locale" that only exists internally for their company or an unsupported locale, they are probably better off using one of the duplicate's solutions combined with [`setlocale`](http://php.net/manual/en/function.setlocale.php) or by using [Carbon](https://carbon.nesbot.com/docs/#api-localization) – apokryfos Aug 24 '18 at 08:13
  • @apokryfos I asked for custom month cause months are different in my language. – Jack potato Aug 24 '18 at 08:18
  • @M.Mon the recommended way is to use localisation methods, that way you can also switch locale easily if you need to. Don't get yourself in the position of implementing your locale from scratch, it's not a game you want to play. – apokryfos Aug 24 '18 at 08:33

1 Answers1

1

Make an array for your month names like so

$montnames = ['', "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dez"];

Trick is to leave first one empty, because months begin with 1 and arrays count at 0.

echo $month[2]; // feb
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
  • You can also use the magic of subtraction. `$arr[$monthNumber-1]`. I would probably use that since I would probably forget why I have a empty value in the array and delete it. – Andreas Aug 24 '18 at 07:55
  • I think a mistake in forgetting the subtraction is bigger and will lead to wrong output. So it is a nice hack. – Markus Zeller Aug 24 '18 at 07:57