2016-11-07 64 views
1

我想通過傳遞到function.here取代「created_at」替換值是我的功能如何在JSON數組用PHP

public function getfeeds($showvalue){ 
     $stmt = $this->con->prepare("SELECT id,image,title,status,profilepic,created_at,url FROM news ORDER BY id DESC "); 
     $stmt->bind_param("s",$showvalue); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 

     $nrow = array(); 
    while ($r = $result->fetch_assoc()) { 

     $nrow[] = $r; 
    } 
     $frow['news'] = $nrow; 
    $json = str_replace("\\/", "/",json_encode($frow)); 
    return $json; 

    } 

我想傳遞給一個函數來代替在創造價值converttime($time)

我的轉換時間函數

function converttime($time, $full = false) { 
    $now = new DateTime; 
    $ago = new DateTime($time); 
    $diff = $now->diff($ago); 

    $diff->w = floor($diff->d/7); 
    $diff->d -= $diff->w * 7; 

    $string = array(
     'y' => 'year', 
     'm' => 'month', 
     'w' => 'week', 
     'd' => 'day', 
     'h' => 'hour', 
     'i' => 'minute', 
     's' => 'second', 
    ); 
    foreach ($string as $k => &$v) { 
     if ($diff->$k) { 
      $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); 
     } else { 
      unset($string[$k]); 
     } 
    } 

    if (!$full) $string = array_slice($string, 0, 1); 
    return $string ? implode(', ', $string) . ' ago' : 'just now'; 
} 

請幫助我,我怎樣才能將它轉換

+0

所以你有一個功能,你知道你想要傳遞的價值......那麼問題究竟是什麼?調用函數並將所需的值傳遞給它。與此無關,您試圖將值綁定到您的語句,但您的查詢中沒有任何參數。這將是一個問題。 –

+0

我想只提取created_在查詢然後傳遞給一個轉換函數,獲取創建的新值,並將其傳遞給json編碼 –

回答

0

讓我們這個直...可變

  1. 呼叫功能:

    $varofcall = $this->converttime($param); 
    

  2. 使用var

    public function getfeeds($showvalue){ 
        $varofcall = $this->converttime(param); 
        $stmt = $this->con->prepare("SELECT id, image, title, status, profilepic, $varofcall ,url FROM news ORDER BY id DESC ");

它呢?

+0

我怎麼能通過該值來轉換時間之前選擇查詢值 –

+0

第一我想從sql查詢中獲取值並將該值傳遞給函數並獲取新值 –

+0

Entendi acho que deve fazer isso depois do $ stmt = $ this-> con> –