views:

122

answers:

2

I'm trying to convert 2010-02 to February, 2010. But, I keep getting December, 1969

I've tried using mktime, strtotime, and some combination of the two, but still haven't been able to do it...

This is what I tried most recently...

$path_title = date('F, Y', mktime(0,0,0,2,0,2010));
A: 

Try this:

$str = '2010-02';
echo date('F, Y',mktime(0,0,0,substr($str,-2),1,substr($str,0,4)));

You have to make sure you use valid values to mktime(). In your example that you edited into the question, you have 0 as the day, which is effectively the first day minus one, which puts you into the previous month.

zombat
+3  A: 

This would be a way to do it:

$dateString = '2010-02';
list($year, $month) = explode('-', $dateString);
$timeStamp = mktime(0, 0, 0, $month, 1, $year);
echo date('F, Y', $timestamp);

Another way would be:

$dateString = '2010-02';
$timestamp = strtotime($dateString . '-01');
echo date('F, Y', $timestamp);

strtotime can't handle ambiguous dates like "2010-02", but if you make it a full date it should work.

Otherwise, you may want to look into something like DateTime::createFromFormat.

deceze
Thanks for this. And for the link, because I think that'll be helpful in the future!
n00b0101