0

is there anyway to convert date month numeric to string.

e.g. for 1 convert to January, 2 => February, etc

i tried below

<?php echo date('F', strtotime($member['dob_month'])); ?>

didnt work out

tonoslfx
  • 3,422
  • 15
  • 65
  • 107
  • 1
    One can make an array... – Your Common Sense Dec 27 '10 at 20:47
  • 1
    What is `$member['dob_month']`? Is it an int 1..12? String? – moinudin Dec 27 '10 at 20:50
  • possible duplicate of [In PHP given a month string such as "November" how can I return 11 without using a 12 part switch statement?](http://stackoverflow.com/questions/2701695/in-php-given-a-month-string-such-as-november-how-can-i-return-11-without-using) – Gordon Dec 27 '10 at 20:54

4 Answers4

5
<?php echo date("F",mktime(0,0,0,$member['dob_month'],1,0); ?>
Michael
  • 1,816
  • 7
  • 21
  • 35
2
$months = array(1 => 'January', 2 => 'February', ...);
echo $months[$member['dob_month']];

Given the value of $member['dob_month'] is an 1-based integer.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
2

You can try to use PHP's DateTime class.

$date = DateTime::createFromFormat('n', $member['dob_month']);
echo $date->format('F');

Note: In createFromFormat, use 'm' if the month has leading zeros, 'n' if it doesn't.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

Your problem is that date() needs a timestamp for it´s second parameter and strtotime($member['dob_month']) does not result in a meaningfull timestamp if $member['dob_month'] is a number from 1 to 12.

You can use something like:

date("F", mktime(0, 0, 0, $member['dob_month'], 1, 2010));
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
jeroen
  • 91,079
  • 21
  • 114
  • 132