Page 1 of 1

Month Number to Month Name Function

Posted: Tue Oct 11, 2011 6:20 pm
by amer4web
// This function  returns month name for the month number submitted as its argument.
//                                                *** By Amer Murshed***
function getMonthName($month) 
  {
   switch ($month) 
   {
    case 1: echo "January"; break;
    case 2: echo "February"; break;
    case 3: echo "March"; break;
    case 4: echo "April"; break;
    case 5: echo "May"; break;
    case 6: echo "June"; break;
    case 7: echo "July"; break;
    case 8: echo "August"; break;
    case 9: echo "September"; break;
    case 10: echo "October"; break;
    case 11: echo "November"; break;
    case 12: echo "December"; break;
    default: echo " Error! The value entered is  not a month number please enter number 1-12.";
 }
}

Re: Month Number to Month Name Function

Posted: Sat Oct 15, 2011 10:31 am
by Kamal
A more efficient (+faster) way:
function getMonthName($month){
    $months = array('-', 'January', 'Februrary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    if(isset($months[$month])) return $months[$month];
    else return '-';
}

Re: Month Number to Month Name Function

Posted: Sat Oct 15, 2011 12:15 pm
by libeco
Or:

Multi language:
function get_month_name($month){
	return strftime('%B', mktime(0, 0, 0, $month, 0, 0));
}
English
function get_month_name($month){
	return date('F', mktime(0, 0, 0, $month, 0, 0));
}

Re: Month Number to Month Name Function

Posted: Sun Oct 16, 2011 10:19 am
by Kamal
libeco wrote:Or:
...
There's SO much you can do with the date function and its relatives!

Re: Month Number to Month Name Function

Posted: Tue Nov 15, 2011 9:16 pm
by robinatorzzz
libeco wrote:Or:

Multi language:
function get_month_name($month){
	return strftime('%B', mktime(0, 0, 0, $month, 0, 0));
}
When i pass in 9, i get august... so maybe this will be better...
function get_month_name($month){
        $month = $month+1;
	return strftime('%B', mktime(0, 0, 0, $month, 0, 0));
}