-1

Is there a a better solution than what I have below to convert a month name to a month number using JavaScript or PHP?

I did like this:

$monNum =  date('n',strtotime($staff->curMonth));
Mat
  • 195,986
  • 40
  • 382
  • 396
Devswa
  • 329
  • 2
  • 4
  • 12

1 Answers1

12

You could keep an object of key/value pairs where the key is the month name and the value is the month number:

var months = {
    January: 1,
    February: 2,
    ...
};

and then:

var monthNumber = months['April'];

or:

var monthNumber = months.April;
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
  • This is really the only sensibly way of doing it, short of a hideous switch statement or if block. – crush Apr 16 '12 at 20:55