Find A Month From A Given Integer With PHP

7th June 2010 - 1 minute read time

If you need to know the month from a given integer (from 1 to 12) then you can use the following snippet. This will return the string "Feb".

date("M", mktime(0, 0, 0, 2));

This can be encapsulated into a function call that will take a number between 1 and 12 and return the corresponding string for that month. This function includes some simple error checking to make sure that the number is valid before trying to work out the date.

  1. function getMonth($month)
  2. {
  3. if (!is_numeric($month) && $month 12) {
  4. return false;
  5. }
  6. return date("M", mktime(0, 0, 0, $month));
  7. }

Add new comment

The content of this field is kept private and will not be shown publicly.