Karthik Malla
Karthik Malla

Reputation: 5800

Fatal error: Call to a member function format() using PHP function

I am trying to convert the date in the following manner and its working fine

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

Now, I tried to convert the date in the following manner and its not working

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

I am getting Fatal error: Call to a member function format()

PHP Class:

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

Why does the $strdate works perfectly and $fetch_resdetails['coupondate']; doesn't work?

even when echo $fetch_resdetails['coupondate']; prints as '1st of September 2014 Mon';

Upvotes: 0

Views: 849

Answers (2)

Parth Kumar
Parth Kumar

Reputation: 179

<?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 );

This is working..can you check with this code..

Upvotes: 1

Suchit kumar
Suchit kumar

Reputation: 11859

<?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');      
}  

this is working.to get your error i sent the $fetch_resdetails['coupondate'] without assigning anything and got the same error.so before sending $fetch_resdetails['coupondate'] check if it hase some value or not.

Upvotes: 1

Related Questions