Roeland
Roeland

Reputation: 3878

How do I convert numeric month number to name of month using PHP

I have a variable with a 2 digit month number. Is there a built in function that allows me to change it to the month name. I thought maybe the date() function would work, but that requires a full timestamp.

Upvotes: 2

Views: 6791

Answers (3)

Onimusha
Onimusha

Reputation: 3385

I use the following which is easier to code when needed many times

$month_names = array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

echo $month_names[3]; //prints March

Useful if you have a header or config file that's included on all your pages

Upvotes: 1

Mark Elliot
Mark Elliot

Reputation: 77104

You can use mktime with arbitrary parameters (exception of month) and then format using date (this might buy you something as far as locales).

 date('M', mktime(0, 0, 0, $month, 1, 2000));

Upvotes: 9

Acron
Acron

Reputation: 1398

Not to my knowledge.

You could use:

<?php
$monate = array(
   1=>"Januar",
   2=>"Februar",
   3=>"März",
   4=>"April",
   5=>"Mai",
   6=>"Juni",
   7=>"Juli",
   8=>"August",
   9=>"September",
   10=>"Oktober",
   11=>"November",
   12=>"Dezember");
?>

<?php
$monat = date("n");
??

Upvotes: -1

Related Questions