[英]Fatal error: Call to a member function format() using PHP function
我正在嘗試以以下方式轉換日期及其工作正常
$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'];
可以完美$fetch_resdetails['coupondate'];
不起作用?
即使echo $fetch_resdetails['coupondate'];
打印為'1st of September 2014 Mon'
;
<?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']並得到了相同的error.so在發送$ fetch_resdetails ['coupondate']之前,先檢查它是否有價值。
<?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 );
這正在工作..您可以檢查此代碼嗎..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.