更新
您的所有方法都有效。谢谢@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
中显示符合所需年龄的成员资格表中的所有内容。
答案 0 :(得分:0)
如果我猜对了,你需要比较几年。 因此,请使用列而不是m,d,y
select FLOOR(DATEDIFF('2013-07-19','2013-05-10'))
答案 1 :(得分: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个字符转换为日期,并将其与当前日期进行比较,并获得数年的差异。
答案 2 :(得分: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>';
}
}
答案 3 :(得分: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);