Codeigniter-如何从数据库中计算生日的年龄,以及如何将其加载到视图中?

问题描述 投票:0回答:1

我对Codeigniter完全陌生。我必须创建一个其中包含“ age”的REST客户端。

这是我的桌子

  CREATE TABLE `teo_profile` (
  `photo` varchar(255) DEFAULT NULL,
  `full_name` varchar(255) NOT NULL,
  `nick` varchar(50) NOT NULL,
  `domicile` varchar(50) NOT NULL,
  `birthday` varchar(11) NOT NULL,
  `reason` text NOT NULL,
  `hope` text NOT NULL,
  `id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

然后我必须在视图上显示'age'。所以我这样输入我的代码

型号:

public function getTeoById($id)
    {
        return $this->db->get_where('teo_profile', ['id' => $id])->row_array();
    }
public function age(){
        $this->load->helper('date');
        $birthday = $this->db->select('birthday');
        $now = time();
        return timespan($birthday, $now, 1); //3th parameter means I just used years type
    }

控制器:

public function detail($id)
    {
        $data['title'] = 'TEO Detail';
        $data['teo'] = $this->Teo_model->getTeoById($id);
        $data['age'] = $this->Teo_model->age();
        $this->load->view('templates/header', $data);
        $this->load->view('teo/detail', $data);
        $this->load->view('templates/footer');
    }

视图:

<p class="card-text"><?= $age ?></p>

然后显示

50年

所以我调试模型,方法age:

$f = timespan($birthday, $now); //I erased 3th parameter to get dmYHis
var_dump($f);die;

然后显示此内容

string(55)“ 50年,3个月,1周,6天,3小时,17分钟”

我该怎么办?我已经尝试过[diff]方法,但是它不起作用。

php mysql codeigniter datetime rest-client
1个回答
0
投票

您应该运行并获得这样的结果。

$birthday = $this->db->select('birthday')->get('teo_profile')->row()->birthday;

阅读更多:Generating Query Results

© www.soinside.com 2019 - 2024. All rights reserved.