tonoslfx
tonoslfx

Reputation: 3442

PHP convert month

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

Upvotes: 0

Views: 4645

Answers (4)

jeroen
jeroen

Reputation: 91792

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));

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227310

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.

Upvotes: 2

Michael
Michael

Reputation: 1886

<?php echo date("F",mktime(0,0,0,$member['dob_month'],1,0); ?>

Upvotes: 5

Dan Lugg
Dan Lugg

Reputation: 20621

$months = array(1 => 'January', 2 => 'February', ...);
echo $months[$member['dob_month']];

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

Upvotes: 2

Related Questions