PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] Get Time from DateTime in CakePHP

 March 18, 2022     cakephp     No comments   

Issue

I have a table which contains the field "created". It is a DateTime type. I am getting the data using:

$this->MyModel->find("all");

I only need the time (12:00:00) from the DateTime (2013-12-27 12:00:00). In SQL it is very easy to do by using DATE_FORMAT() but I have no Idea how to do this in cake... I want to do this in the model by getting the data from DB and not one by one in the view using TimeHelper.


Solution

You can use a virtual field

class MyModel extends AppModel {

    var $virtualFields = array(
            'time' => 'DATE_FORMAT(myModel.date_time,"%H/%i/%s")');

    //the rest of the class
}


Answered By - Kai
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing