-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
  • 202,337
  • 40
  • 393
  • 406
Devswa
  • 327
  • 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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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