Month Number to Month Name Function

Written something you are proud of, post it here.
Post Reply
amer4web
Posts: 4
Joined: Tue Oct 11, 2011 6:02 pm

Month Number to Month Name Function

Post by amer4web »

[syntax=php]// 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.";
}
}[/syntax]
Last edited by jacek on Tue Oct 11, 2011 6:25 pm, edited 1 time in total.
Reason: code tags...
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Month Number to Month Name Function

Post by Kamal »

A more efficient (+faster) way:
[syntax=php]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 '-';
}[/syntax]
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Month Number to Month Name Function

Post by libeco »

Or:

Multi language:
[syntax=php]function get_month_name($month){
return strftime('%B', mktime(0, 0, 0, $month, 0, 0));
}[/syntax]

English
[syntax=php]function get_month_name($month){
return date('F', mktime(0, 0, 0, $month, 0, 0));
}[/syntax]
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Month Number to Month Name Function

Post by Kamal »

libeco wrote:Or:
...

There's SO much you can do with the date function and its relatives!
robinatorzzz
Posts: 8
Joined: Mon Jul 18, 2011 10:15 pm

Re: Month Number to Month Name Function

Post by robinatorzzz »

libeco wrote:Or:

Multi language:
[syntax=php]function get_month_name($month){
return strftime('%B', mktime(0, 0, 0, $month, 0, 0));
}[/syntax]


When i pass in 9, i get august... so maybe this will be better...
[syntax=php]function get_month_name($month){
$month = $month+1;
return strftime('%B', mktime(0, 0, 0, $month, 0, 0));
}[/syntax]
Image
Post Reply