致命错误:使用PHP函数调用成员函数format()

时间:2014-09-01 05:24:26

标签: php datetime

我正在尝试按以下方式转换日期及其正常工作

$strdate = '1st of September 2014 Mon';
contime($strdate);

现在,我尝试按以下方式转换日期并且无法正常工作

$strdate = $fetch_resdetails['coupondate'];
contime($strdate);

我正在Fatal error: Call to a member function format()

PHP类:

function contime($gettime) {
        $date = DateTime::createFromFormat("jS \of F Y D", $gettime);
        return $date->format('Ymd');      
}

为什么$strdate完美无缺,$fetch_resdetails['coupondate'];不起作用?

即使echo $fetch_resdetails['coupondate'];打印为'1st of September 2014 Mon';

2 个答案:

答案 0 :(得分:1)

<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
$strdate = '1st of September 2014 Mon';
echo contime($strdate);
$fetch_resdetails['coupondate']="1st of September 2014 Mon";

$strdate = $fetch_resdetails['coupondate'];
echo contime($strdate);

function contime($gettime) {
        $date = DateTime::createFromFormat("jS \of F Y D", $gettime);
        return $date->format('Ymd');      
}  

这是有效的。为了得到你的错误我发送$ fetch_resdetails ['coupondate']而没有分配任何内容并得到相同的错误。所以在发送$ fetch_resdetails ['coupondate']之前检查它是否有某种价值。< / p>

答案 1 :(得分:1)

<?php
function contime($gettime) {
    $date = DateTime::createFromFormat("jS \of F Y D", $gettime);
    return $date->format('Y-m-d');      
}
$fetch_resdetails = array( 'getdate'    => '1st of September 2014 Mon' );
$strdate = $fetch_resdetails['getdate'];
$date = contime($strdate);
print_r( $date );

这是有效的..你可以查看这段代码..