In my AppController beforeFilter i am saving user data to variables for use in my view. I need to grab the last_login field that is a datetime and then use the dateformat function to display the date i want but cakephp is outputting this variable as a string, is there a way to putput as the format of the table? (datetime).
// 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);
View
<?php echo date_format($lastLogin, 'F jS \at h:i:s A'); ?>
Error
Warning (2): date_format() expects parameter 1 to be DateTime, string given
Firstly your code is messy. Try this:
Cake has a class for doing dates and times.
Generally you should do the data to string in the view, making it part of the controller or model will have a negative impact on your application if you need to do other things with the data.