我想将数字转换为实际月份。我只需要一个月。
我现在正在尝试这样
$str = 8;
$anInt = intval($str);
$test = date('M',$anInt);
echo $test;
结果是 JAN,结果我应该得到"AUG"。我不知道为什么。
对我有什么想法或建议吗?
date()
函数假定时间戳。
你应该试试这个:
$test = date('M',mktime(0,0,0,$anInt,0,0);
从日期和 mktime 的文档:
// Prints: Your date's month is : August
echo "Your date's month is : " . date("F", mktime(0, 0, 0, $anInt, 0, 0));
使用 mktime
.
echo date('M', mktime(0, 0, 0, $str));
你在这里:
echo date('M',strtotime('08/01/2012'));
或者,如果您想要全部大写:
echo strtoupper(date('M',strtotime('08/01/2012')));
可能还有其他方法,但这是首先想到的方法
重复的问题。在这里阅读。
在此处阅读日期函数。
使用date()
您应该使用时间戳。要将日期值转换为时间戳,请使用 strtotime
$date = '2012-8-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);
对于您的问题:
$monthnumber = 4;
$date = '2012-'.$monthnumber.'-8';
$timestamp = strtotime($date);
echo date('M',$timestamp);