PHP Month Number to Month Name

By gilbitron in PHP on 31/03/2009 at 03:17

Views: 4483
Tagged: php, date

Starter:

An easy way to convert a month number to a month name without a switch statement.

Main Course:

$monthName = date("F", mktime(0, 0, 0, $monthNum, 10)); 

Expand Report Code | Install Coda Clip

Please log in to vote.

Compliments to the Chef

  1. Comment by http://www.myopenad.com on 30/05/2009 at 03:07

    http://www.myopenad.com

  2. Comment by Christopher Shennan on 27/07/2009 at 07:41

    Fantastic... This was a nice quick solution to my problem. I was after the month names from November to Aprils so I tweaked this as follows:

    for($i=1;$i

  3. Comment by Christopher Shennan on 27/07/2009 at 07:42

    Fantastic... This was a nice quick solution to my problem. I was after the month names from November to Aprils so I tweaked this as follows:

    for($i=1;$i<=6;$i++) {
    echo date("F", mktime(0, 0, 0, (($i+10) % 12), 10));
    }

  4. Comment by emeacham on 28/05/2010 at 07:07

    mktime parameter count is off a bit for correct date creation.

    PHP manual says:
    int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

    Anyone trying to copy and paste the exact code above, add a day (0, ) after $monthNum in mktime.

  5. Comment by emeacham on 28/05/2010 at 07:21

    errr... I fell into the same trap of not testing code before posting, too... :P

    the 0 should be a 1... and I would stick with 1, and not try to be funny and go with the last day of the month, since every month has a first day (hence the 1) and not every day has 29 through 31.

    Cheers :)

  6. Comment by Chad G on 20/06/2010 at 08:54

    Great tip, needed this for my site.

How was your Meal?


  • Allowed HTML: <b><strong><u><i><em><a>

  • Why ask? It helps us stop spam comments.