Php 为什么DateTime格式在我的第二个日期格式中作为非对象返回?

Php 为什么DateTime格式在我的第二个日期格式中作为非对象返回?,php,datetime,format,Php,Datetime,Format,我得到一个错误,该错误仅显示此DateTime格式函数是否被第二次调用。它在第一个调用上运行良好,这是我假设的,因为错误只发生在第二个调用的线路上 if( ! function_exists('month_dropdown')){ function month_dropdown($field_name = 'month', $selected = '01', $value_format = 'm', $atts = ''){ for($i=1;$i<=12;$i++

我得到一个错误,该错误仅显示此DateTime格式函数是否被第二次调用。它在第一个调用上运行良好,这是我假设的,因为错误只发生在第二个调用的线路上

if( ! function_exists('month_dropdown')){
    function month_dropdown($field_name = 'month', $selected = '01', $value_format = 'm', $atts = ''){
        for($i=1;$i<=12;$i++){
            $numObj = DateTime::createFromFormat('!'.$value_format, $i);
            $val = $numObj->format($value_format);
            $nameObj = DateTime::createFromFormat('!F', $i);
            $text = $nameObj->format('F');
            $months[$val] = $text;
        }
        return form_dropdown($field_name, $months, $selected, $atts);
    }
}

第73行是定义变量
$text
的地方。

您使用了错误的格式字符。对于月份整数,使用
n

m和n

月份的数字表示形式,带或不带前导零

示例:01到12或1到12

比如说

$nameObj = DateTime::createFromFormat('!n', $i);
$text = $nameObj->format('F');

在这里演示~

显示了什么?您确实知道
F
格式代表了什么,对吗?一个月的文本表示,如一月或九月。您似乎试图将其与整数一起使用,因为它应该将1转换为1,等等。为什么我会被否决?
$nameObj = DateTime::createFromFormat('!n', $i);
$text = $nameObj->format('F');