2016-08-17 1 views
2

용서해주세요. 나는이 질문에 대한 모든 배경을주고 싶다.

프로젝트에 서버 측 처리 DataTables을 사용하고 있습니다. 내 목표는 echo 함수를 echo에 사용하여 DateTime 유형 인 CreatedDate라는 데이터베이스의 열에서 오늘 날짜와 날짜 사이의 일 수를 사용하는 것입니다.

내가 처음 DataTables의 외부에서 작업 기본 기능을 얻으려고 노력하였으며, 함수가 아래의 의도와 $date$date 사이의 일 수의 $current_date 출력으로 작동 15 일 ","16 일 라이브 " 내가의 오류를 잡 유지

// date variable 
$created_date = 'CreatedDate'; 
$current_date = new DateTime(); 

// array of database columns which should be read and sent back to DataTables. 
// the 'db' parameter represents the column name in the database, while the 'dt' 
// parameter represents the DataTables column identifier. 
$columns = array(
array('db' => 'Client', 'dt' => 0), 
array('db' => 'EstimateNumber', 'dt' => 1), 
array('db' => 'Status', 'dt' => 2), 
array('db' => 'CurrentEstimateTotal', 'dt' => 3), 
array(
    'db' => $created_date, 
    'dt' => 4, 
    'formatter' => 
     function ($created_date, $row) use ($current_date) { 
     $date = new DateTime($created_date); 
     $diff = date_diff($date, $current_date); 
     echo $diff->format('%d days live'); 
    } 
) 
); 

: 등, "라이브 : 내 DataTables $columns 배열에서

$sql = "SELECT CreatedDate FROM Estimates"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
// output data of each row 
while($row = $result->fetch_assoc()) { 
    $created_date = $row["CreatedDate"]; 
    $date = new DateTime($created_date); 
    $current_date = new DateTime(); 
    $diff = date_diff($date, $current_date); 
    echo $diff->format('%d days live'); 
} 
} else { 
echo "No results"; 
} 

를, 나는 같은 효과를 달성하기 위해 노력하고

PHP Notice: Trying to get property of non-object.

어떻게 해결할 수 있습니까? 문제가 무엇인지 확인하기 위해 DataTables 배열에 $created_date$date 변수의 var_dump()를 가져 오는 데 어려움을 겪고 있습니다.

DataTables 서버 측 처리에 대한 자세한 내용은 I am using this template as a base for my DataTables script. DataTables 스크립트에서도 this helper class.을 사용 해 주셔서 감사합니다.

답변

1

콜백에서 문자열을 반환하십시오.

'formatter' => 
     function ($created_date, $row) use ($current_date) { 
     $date = new DateTime($created_date); 
     $diff = date_diff($date, $current_date); 
     return $diff->format('%d days live'); //That will solve your problem 
    } 
+0

고맙습니다! 왜'echo' 대신에'return'을해야합니까? – Liz

+0

클로저가 뭔가를 반환해야하므로 'datatable.com'코드의 어딘가 깊은 곳에서 어떤 객체를 기반으로 시작합니다. 우리가 일반적으로 결과를 필요로하므로 반환하지 않고 클로저를 이미징하는 것은 어렵습니다 :) – jaro1989

+0

그건 매력처럼 작동했습니다! 고맙습니다! – Liz

관련 문제