$res['total_times_borrowed'] = 0;
$total_time = 0;
if ($get_books_qry->num_rows > 0) {
    while ($book = $get_books_qry->fetch_assoc()) {
        $res['total_times_borrowed']++;
        if ($book['timediff'] == null) {
            $total_time += time() - strtotime($book['outDate']);
        } else {
            $total_time += $book['timediff'];
        }
    }
} else {
    //The user hasn't borrowed any books
}
//Calculate the total time the book has been borrowed (in a readable time format)
$res['total_time_borrowed'] = convertSecondsToReadable($total_time);
//Add a contact array to the result
$res['contact'] = array();
//Find the link between the user and the contact entries
$get_user_contact = "SELECT * FROM lib_User_Contact WHERE userID = '" . $res['userID'] . "'";
$get_user_contact_qry = $conn->query($get_user_contact);
if ($get_user_contact_qry->num_rows > 0) {
    //Loop through all contact rows that are related to the user
    while ($user_contact = $get_user_contact_qry->fetch_assoc()) {
        //Get the contact info from the id
        $get_contact = "SELECT * FROM lib_Contact WHERE contactID='" . $user_contact["contactID"] . "'";
        $get_contact_qry = $conn->query($get_contact);
        if ($get_contact_qry->num_rows > 0) {
            if ($contact = $get_contact_qry->fetch_assoc()) {
                //Add the information to the result array
                $res['contact'][] = array('phone' => $contact['phone'], 'email' => $contact['email']);
$xx = 0;
$total_time = 0;
if ($get_books_qry->num_rows > 0) {
    while ($book = $get_books_qry->fetch_assoc()) {
        $xx++;
        if ($book['timediff'] == null) {
            $total_time += time() - strtotime($book['outDate']);
        } else {
            $total_time += $book['timediff'];
        }
    }
} else {
    //echo "No books borrowed";
    j_die($error['no_lended_books']);
}
$res["total_time_lended"] = convertSecondsToReadable($total_time);
$res["total_times_lended"] = $xx;
echo json_encode($res);
function convertSecondsToReadable($seconds)
{
    //Find difference in time in a readable format
    $dtF = new DateTime("@0");
    $dtT = new DateTime("@{$seconds}");
    $readable["months"] = (int) $dtF->diff($dtT)->format('%m');
    $readable["days"] = (int) $dtF->diff($dtT)->format('%a');
    $readable["hours"] = (int) $dtF->diff($dtT)->format('%h');
    $readable["minutes"] = (int) $dtF->diff($dtT)->format('%i');
    $readable["seconds"] = (int) $dtF->diff($dtT)->format('%s');
    $result = "";
    $prev_value = false;
    return $readable;
            }
        } else {
            //No feedback :O
        }
        //Calculate average amount of stars
        $average_stars = 0;
        if (count($feedback['stars']) > 0) {
            $total_stars = 0;
            for ($i = 0; $i < count($feedback['stars']); $i++) {
                $total_stars += intval($feedback['stars'][$i]['value']);
            }
            $average_stars = $total_stars / count($feedback['stars']);
        }
        $feedback['average_stars'] = $average_stars;
        //Print book info
        $res['book'] = array('ISBN10' => $book['ISBN10'], 'ISBN13' => $book['ISBN13'], 'total_lend_time' => convertSecondsToReadable($total_lended_time), 'total_lend_times' => count($borrowers), 'borrowers' => $borrowers_v2, 'feedback' => $feedback);
        echo json_encode($res);
    } else {
        //Query failed (nonexistant book or SQL error)
        j_die($error['nonexistant_book']);
    }
} else {
    //Query failed (nonexistant book or SQL error)
    j_die($error['nonexistant_book']);
}
function convertSecondsToReadable($seconds)
{
    //Find difference in time in a readable format
    $dtF = new DateTime("@0");
    $dtT = new DateTime("@{$seconds}");
    $readable["months"] = (int) $dtF->diff($dtT)->format('%m');