ShoeLace1291 Posted July 29, 2012 Share Posted July 29, 2012 I am trying to convert a number of the month to the name of the month... For example: 12 would convert to December. What's the best way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/266414-finding-the-name-of-a-month-based-on-number/ Share on other sites More sharing options...
xyph Posted July 29, 2012 Share Posted July 29, 2012 Using an array, and keys. <?php $month_names = array("","January","February","March","April","May","June","July","August","September","October","November","December"); echo $month_names[$month_number]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266414-finding-the-name-of-a-month-based-on-number/#findComment-1365261 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2012 Share Posted July 29, 2012 I am trying to convert a number of the month to the name of the month... For example: 12 would convert to December. What's the best way to do this? Context would be helpful since there's probably more than one "best way". Quote Link to comment https://forums.phpfreaks.com/topic/266414-finding-the-name-of-a-month-based-on-number/#findComment-1365264 Share on other sites More sharing options...
rythemton Posted July 29, 2012 Share Posted July 29, 2012 Context would be helpful since there's probably more than one "best way". I agree, there is always more than one way to do things. If you don't feel like writing out the names, you could use: echo date( 'F', mktime( 0, 0, 0, $month_number ) ); Probably not as efficient, but less typing. Quote Link to comment https://forums.phpfreaks.com/topic/266414-finding-the-name-of-a-month-based-on-number/#findComment-1365306 Share on other sites More sharing options...
ManiacDan Posted July 30, 2012 Share Posted July 30, 2012 strftime() also respects the current locale, so if you use that you can get the french/german/spanish month names easily. Quote Link to comment https://forums.phpfreaks.com/topic/266414-finding-the-name-of-a-month-based-on-number/#findComment-1365407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.