Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String CakePHP视图变量作为日期时间而不是字符串_String_Cakephp_Variables_Datetime - Fatal编程技术网

String CakePHP视图变量作为日期时间而不是字符串

String CakePHP视图变量作为日期时间而不是字符串,string,cakephp,variables,datetime,String,Cakephp,Variables,Datetime,在我的AppController beforeFilter中,我将用户数据保存到变量中,以便在视图中使用。我需要抓取last_login字段,它是一个datetime,然后使用dateformat函数来显示我想要的日期,但是cakephp将这个变量作为字符串输出,是否有方法将putput作为表的格式?(日期时间) 看法 首先,你的代码很混乱。试试这个: $this->User->id = $this->Auth->user('id'); $this->set('la

在我的AppController beforeFilter中,我将用户数据保存到变量中,以便在视图中使用。我需要抓取last_login字段,它是一个datetime,然后使用dateformat函数来显示我想要的日期,但是cakephp将这个变量作为字符串输出,是否有方法将putput作为表的格式?(日期时间)

看法


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

$this->User->id = $this->Auth->user('id');
$this->set('lastLogin', $this->User->field('last_login'));
蛋糕有做日期和时间的功能

echo CakeTime::niceShort($lastLogin);
通常,您应该将数据转换为视图中的字符串,如果您需要对数据执行其他操作,则将其作为控制器或模型的一部分将对应用程序产生负面影响

Warning (2): date_format() expects parameter 1 to be DateTime, string given
$this->User->id = $this->Auth->user('id');
$this->set('lastLogin', $this->User->field('last_login'));
echo CakeTime::niceShort($lastLogin);