2014-09-01 97 views
0

我想通過以下方式將日期轉換調用一個成員函數的格式()和它的做工精細致命錯誤:使用PHP函數

$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';

+0

之前'$返回日期....''添加如果($日期===假){的print_r(日期時間::輸出 – 2014-09-01 05:27:58

+0

輸出Array([warning_count] => 0 [warnings] => Array()[error_count] => 1 [errors] => Array([0] => >數據丟失))' – lock 2014-09-01 05:31:23

回答

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

這是working..can你這段代碼檢查..

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

這是working.to讓你的錯誤我送$ fetch_resdetails [「coupondate」]沒有任何分配和發送$ fetch_resdetails [「coupondate」]檢查前得到了同樣的error.so如果HASE一定的價值或不。