0

Possible Duplicate:
In PHP given a month string such as “November” how can I return 11 without using a 12 part switch statement?

How can I convert month name to month number in php without using an array? Is there any inbuilt function available in php?

Eg: If I use "January" from the variable, it should change to "01".

Community
  • 1
  • 1
user1010966
  • 473
  • 1
  • 6
  • 13

2 Answers2

5
$monthName = 'January';
$date = date_parse($monthName);
$monthNumber = $date['month'];

Note that this will return 1, not 01, so change your display format accordingly.

user703016
  • 37,307
  • 8
  • 87
  • 112
1

I'd do something like this as there are no Enum in PHP as far as I know.

class Months
{
    const January  = 1;
    const Feburary = 2;
    // etc.
}

var $currentMonth = Months::January;

/J

Jontatas
  • 954
  • 7
  • 17