dongyu8694 2011-04-09 14:46
浏览 72
已采纳

基于dateOfBirth mysql时间戳显示年龄的问题

I've got a site with user profiles. On the home page I am trying to show little summaries of each person's profile. This summary includes the name of the person, the gender and the age of the person.

        <ul>                
            {section name=i loop=$person_list}
                <li><a href="user_detail.php?id={$person_list[i].id}">{$person_list[i].username}, {$person_list[i].gender}, {$age[i]}
            {/section}
        </ul>

I previously used a foreach loop to run through them, but I need the index to also show the display age calculated in the index.php file.

if (isset($_GET['name']) && isset($_GET['gender'])) 
{
   $name = $_GET['name'];
   $gender = $_GET['gender'];
} 
else 
{
   $name = "";
   $gender = "";
}

define("PERSONS_PER_PAGE", 10);


if (isset($_GET['offset'])) 
{
    $offset = $_GET['offset'];
} 
else 
{
    $offset = 0;
}


list($person_list, $num_persons) = get_persons($name, $gender, $offset, PERSONS_PER_PAGE);

$ages = array();


for ($i = 0; $i < count($person_list); $i++)
{
    $ages[$i] = age($person_list[$i].$dateOfBirth, time());
}

$smarty = new Smarty;
$smarty->assign("name", $name);
$smarty->assign("gender", $gender);
$smarty->assign("persons_per_page", PERSONS_PER_PAGE);
$smarty->assign("interests", $interests);
$smarty->assign("offset", $offset);
$smarty->assign("num_persons", $num_persons);
$smarty->assign("person_list", $person_list);
$smarty->assign("ages", $ages);
$smarty->display("index.tpl");

Don't worry too much about the $offset and PERSONS_PER_PAGE, that's only used for pagination.

My age function looks like this:

function age($birth)
{
   $birth_date = new DateTime();
      $birth_date = $birth;
   $birth_date->setTimestamp($birth);

   $now = time();

   $now_date = new DateTime();
   $now_date->setTimestamp($now);

   $interval = $birth_date->diff($now_date); // $interval is a DateInterval


   $age = $interval->y; // number of years in the interval
   return $age;
}

Additionally, in case it's relevant; my get_persons function looks like this:

function get_persons($name, $gender, $offset, $persons_per_page) 
{
    $connection = mysql_open();

    $query = "SELECT SQL_CALC_FOUND_ROWS id, username, gender, dateOfBirth FROM Person";
    if ($name && $gender) 
    {
        $query .= " WHERE name like '%$name%' AND gender like '%$gender%'";
    }
    $query .= " order by id";
    $query .= " LIMIT $offset, $persons_per_page";
    // print "$query<br>
";

    $result = mysql_query($query, $connection) or show_error();

    $r = mysql_query("SELECT FOUND_ROWS()", $connection) or showerror();
    $r = mysql_fetch_array($r);
    $num_entries = $r[0];

    $entries = array();

    $person_list = array();
    while ($person = mysql_fetch_array($result)) {
        $person_list[] = $person;
    }

    mysql_close($connection) or show_error();
    return array($person_list, $num_entries);
}

and the table for a person looks like this:

create table if not exists Person
(
  id int not null auto_increment primary key,
  username varchar(10) not null,
  name varchar(40) not null,
  gender varchar(1) not null,
  dateOfBirth timestamp not null,
  email varchar(40) not null
);

This produces the following error:

Notice: Undefined variable: dateOfBirth in /net/homes.ict.griffith.edu.au/export/home/s2737451/public_html/wp/labs/lab5/index.php on line 39 Fatal error: Call to a member function setTimestamp() on a non-object in /net/homes.ict.griffith.edu.au/export/home/s2737451/public_html/wp/labs/lab5/includes/defs.php on line 167

Thanks in advance, I really need help on this :(

  • 写回答

3条回答 默认 最新

  • dongyong6332 2011-04-09 17:09
    关注

    or you can do it by changing your sql command:

    function get_persons($name, $gender, $offset, $persons_per_page) 
    {
        $connection = mysql_open();
    
       $query = "SELECT SQL_CALC_FOUND_ROWS id, username, gender, (YEAR(NOW())-YEAR(dateOfBirth)) as age FROM Person";
        if ($name && $gender) 
        {
            $query .= " WHERE name like '%$name%' AND gender like '%$gender%'";
        }
        $query .= " order by id";
        $query .= " LIMIT $offset, $persons_per_page";
    
        $result = mysql_query($query, $connection) or show_error();
    
        $r = mysql_query("SELECT FOUND_ROWS()", $connection) or showerror();
        $r = mysql_fetch_array($r);
        $num_entries = $r[0];
    
        $entries = array();
    
        $person_list = array();
        while ($person = mysql_fetch_array($result)) {
            $person_list[] = $person;
        }
    
        mysql_close($connection) or show_error();
        return array($person_list, $num_entries);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名