0

更新

你所有的方法都有效。谢谢@NiloySaha、@AeroX、@Ray、@SusheelMishra。

但是如果我想比较更多的领域,比如工作经验、薪水等呢?@Ray 我只是像这样添加更多参数

function check_job_adverts($age, $experience, $salary){

}

?

或者我将如何处理这个?我对此很陌生,所以如果我问一些无聊的问题,请原谅我。


我有以下内容:

function age() {
        $sql = "SELECT * FROM membership";
        $query = $this->db->query($sql)->result();
        foreach ($query as $row) {
            $id = $row->id_number;
            $dobs = substr($id, 0, 6);
            $dob = str_split($dobs, 2);
            $day = date('d', mktime(0, 0, 0, 0, $dob[2], 0));
            $month = date('m', mktime(0, 0, 0, $dob[1] + 1, 0, 0));
            $year = date('o', mktime(0, 0, 0, 0, 0, $dob[0] + 1));
            $date = "$day/$month/$year";
            //explode the date to get month, day and year
            $date = explode("/", $date);
            //get age from date or birthdate
            $age = (date("md", date("U", mktime(0, 0, 0, $date[0], $date[1], $date[2]))) > date("md") ? ((date("Y") - $date[2]) - 1) : (date("Y") - $date[2]));
            return $age;
        }

我想知道如何在另一个查询中使用 $age 变量。例如:我想$age与一个job_advert可以有年龄要求的表进行比较,然后在会员表中显示所有满足年龄要求的表job_advert

4

4 回答 4

0

如果我猜对了,您需要比较年份。所以使用 column 而不是 m, d, y

select FLOOR(DATEDIFF('2013-07-19','2013-05-10'))
于 2013-07-19T11:11:41.110 回答
0

您可以使用 mysql 来计算人的年龄,而不是在 PHP 中进行计算,从而无需运行多个查询。

SELECT YEAR( DATEDIFF( CURDATE(), DATE(CONCAT( LEFT(id_number,2), '-', MID(id_number,3,2), '-', MID(id_number,5,2) ))) ) AS AGE

像上面这样的东西应该可以将 id_number 的前 6 个字符转换为日期,并将其与当前日期进行比较,并获得年份差异。

于 2013-07-19T11:44:28.210 回答
0

尝试 :

function age() {
    $data   = array();
    $sql = "SELECT * FROM membership";
    $query = $this->db->query($sql)->result();
    foreach ($query as $key=>$row) {
        $id = $row->id_number;
        $dobs = substr($id, 0, 6);
        $dob = str_split($dobs, 2);
        $day = date('d', mktime(0, 0, 0, 0, $dob[2], 0));
        $month = date('m', mktime(0, 0, 0, $dob[1] + 1, 0, 0));
        $year = date('o', mktime(0, 0, 0, 0, 0, $dob[0] + 1));
        $date = "$day/$month/$year";
        //explode the date to get month, day and year
        $date = explode("/", $date);
        //get age from date or birthdate
        $age = (date("md", date("U", mktime(0, 0, 0, $date[0], $date[1], $date[2]))) > date("md") ? ((date("Y") - $date[2]) - 1) : (date("Y") - $date[2]));
        #return $age;                       #not returning the age now instead inserting in a array
        $data[$key]['id'] = $id;
        $data[$key]['age'] = $age;
    }
    $data = $this->another_funcion_call($data);     #calling another function which will recieve the array of ids / age
    return $data;
}

function another_funcion_call($arrayOfIdsAge){
    //loop through the array and do something
    foreach($arrayOfIdsAge as $key=>$each){
        echo "ID :".$each['id'].' AGE : '.$each['age'].'<br>';
    }
}
于 2013-07-19T11:51:54.093 回答
0

如果您不想在与 age() 相同的函数中调用它,这只是一个示例;

function age() {
    $sql = "SELECT * FROM membership";
    $query = $this->db->query($sql)->result();
    foreach ($query as $row) {
        $id = $row->id_number;
        $dobs = substr($id, 0, 6);
        $dob = str_split($dobs, 2);
        $day = date('d', mktime(0, 0, 0, 0, $dob[2], 0));
        $month = date('m', mktime(0, 0, 0, $dob[1] + 1, 0, 0));
        $year = date('o', mktime(0, 0, 0, 0, 0, $dob[0] + 1));
        $date = "$day/$month/$year";
        //explode the date to get month, day and year
        $date = explode("/", $date);
        //get age from date or birthdate
        $age = (date("md", date("U", mktime(0, 0, 0, $date[0], $date[1], $date[2]))) >     date("md") ? ((date("Y") - $date[2]) - 1) : (date("Y") - $date[2]));
        return $age;
    }

function check_job_adverts($age){
// here goes your job_advert checking function
// example.. run an array to match ages vs jobs
// mind the array return if multiple options
}




$age = age();
// somewhere else in your script make the call with $age
$fetchJobs = check_job_adverts($age);
于 2013-07-19T12:36:17.420 回答