开发者

php - mktime or strtotime?

开发者 https://www.devze.com 2022-12-20 01:41 出处:网络
I\'m trying to convert 2010-02 to February, 2010.But, I开发者_如何学C keep getting December, 1969

I'm trying to convert 2010-02 to February, 2010. But, I开发者_如何学C 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));


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消