1

在我的 AppController beforeFilter 中,我将用户数据保存到变量中以便在我的视图中使用。我需要获取作为日期时间的 last_login 字段,然后使用 dateformat 函数显示我想要的日期,但是 cakephp 将此变量输出为字符串,有没有办法将表格的格式作为输入?(约会时间)。

        // Set user variables for each page load
        $id = $this->Auth->user('id');
        // Set $id
        $user_data = $this->User->findById($id);
        // Set User Last Login
        $user_lastlogin = $user_data['User']['last_login'];
        $this->set('lastLogin', $user_lastlogin);

看法

<?php echo date_format($lastLogin, 'F jS \at h:i:s A'); ?>

错误

Warning (2): date_format() expects parameter 1 to be DateTime, string given
4

1 回答 1

0

首先,您的代码很混乱。试试这个:

$this->User->id = $this->Auth->user('id');
$this->set('lastLogin', $this->User->field('last_login'));

Cake 有一门课用来做日期和时间。

echo CakeTime::niceShort($lastLogin);

通常,您应该将数据字符串化到视图中,如果您需要对数据执行其他操作,使其成为控制器或模型的一部分将对您的应用程序产生负面影响。

于 2012-12-13T21:25:10.167 回答