tags:

views:

151

answers:

6

Is there a function in php wherein you can convert the number 12 to its equivalent in a month. For example if the mysql database stores digits and not words for dates. how do you convert the number 12 into the word december?

+4  A: 

You could do like:

echo date('F', mktime(0, 0, 0, 12));
Sarfraz
-1 - OP is looking for month names
symcbean
@symcbean: I did not notice that but fixed before seeing your comment. Thanks
Sarfraz
The code will work fine, so long as the script is only ever executed in January.
salathe
@Sarfraz: `foreach (range(1,12) as $num) echo date('F', strtotime($num));` gives `JanuaryJanuaryJanuaryJanuaryJanuaryJanuaryJanuaryJanuaryJanuaryJanuaryJanuaryJanuary`
salathe
@salathe: hmm that's right, unfortunately i only gave try to digit 1, thanks for that, fixed now.
Sarfraz
@Sarfraz: You're welcome. Aside: why this answer was accepted when it didn't do what was asked, I've no idea!
salathe
@salath: yup, the answer of St. John Johnson should be accepted still.
Sarfraz
+1  A: 
date("F", mktime(0, 0, 0, 12, 1, 2000));
rebus
+10  A: 

Try this and look at the date function for more answers:

date('F', mktime(0, 0, 0, 12))
St. John Johnson
+2  A: 
strftime("%B", mktime(0, 0, 0, 12));

It's like date() except it will take care of localization for you, if you set a locale using setlocale beforehand.

Manos Dilaverakis
+1  A: 

Use this code:

echo date("F", mktime(0, 0, 0, $month, 1, 2010));

Where $month is your number from 1 to 12.

Read more at php functions reference: date

Andrius
+2  A: 

You could also do that directly in MySQL with

SELECT MONTHNAME(STR_TO_DATE(12, '%m')); -- December

See http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

Gordon
Or in PHP-land (as of PHP 5.3) if for some reason doing it in MySQL isn't possible: `DateTime::createFromFormat('n', $num)->format('F')`
salathe