<?php
/**
 * @copyright 2007 Yumenokunisha
 * @license   http://www.php.net/license/3_01.txt PHP License 3.01
 */

/**
 * 現在のSNS登録者数を取得する
 * 
 * @return int
 */
function db_analysis_members_row()
{
    $sql = 'SELECT COUNT(*) FROM c_member';
    $result = db_get_one($sql);

    return $result;
}

/**
 * 現在のSNS登録者の平均年齢を計算する
 * 
 * @return int
 */
function db_analysis_members_average()
{
    $sql = 'SELECT * FROM c_member';
    $list = db_get_all($sql);

    foreach ($list as $item) {
        $birth_year = $item['birth_year'];
        $birth_month = $item['birth_month'];
        $birth_day = $item["birth_day"];
        $age = date("Y") - $birth_year;
        if (date("n") < $birth_month) {
            $age--;
        }
        elseif (date("n") == $birth_month) {
            if (date("j") < $birth_day) {
                $age--;
            }
        }
        $total_age = $total_age + $age;
    }
    
    $sql = 'SELECT COUNT(*) FROM c_member';
    $members_row = db_get_one($sql);
    $members_average = round($total_age / $members_row, 1);

    return $members_average;
}

/**
 * 現在のログイン者数を取得する
 * 
 * @return int
 */
function db_analysis_recent_login_members_row($u)
{
    $sql = 'SELECT COUNT(*) FROM c_member WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(access_date) <= 3600 AND c_member_id <> ' . $u;
    $result = db_get_one($sql);
    $result++;

    return $result;
}

?>