Cake PHP 3.7 Date

I have this database column expense_date [date] when I fetch data and convert to JSON I get
{“expense_date”: “2019-04-16T00:00:00”} I don’t expect “T00:00:00”. How do I get rid of that?



If that dont work:

date_format($row->your_field, 'Y-m-d')

The poster said they’re working with JSON. That would involve auto-conversion of dates to strings, not manual use of formatting functions.

I mis-read, sorry. If you don’t need the time maybe you could just store date in the database without time.

Not exactly sure of all the formats you need.

When CakePHP reads a date column into an entity, though, it turns into a thing with 00:00:00 as the time. Here’s a thorough review of some solutions. Not sure if any will apply to JSON conversion, but I think so…

So far I have never had a problem putting a date only in json, use the php library, not cake.

Here is a c# example using Newtonsoft.Json

dynamic jsonobj = new Newtonsoft.Json.Linq.JObject();
    jsonobj.petid = petid;
    jsonobj.petname = petname;
    jsonobj.odate = odate;
    jsonobj.ocheck = ocheck;
    string jsonText = jsonobj.ToString();

This is just a test database.

It returns:

{ "petid": "70", "petname": "Rover", "odate": "02/27/1990", "ocheck": "1" }

Notice the date:

You cannot store a datetime and expect just a date to be returned. You have to store a date only.

Testing in laravel, I also get correct results, but that’s using php.

If cakephp is not returning correct results, do a PR.


I just tested in cakephp ver 4

$pets = Pet::getPets($pages->getLimit2(), $pages->getPerpage());
        $json = json_encode($pets);
        echo $json;

Here is what I got:

{"petid":"9","petname":"BIGCAT","species":"CAT","sex":"F","ownerid":"4","petowner":"FRED","ostreet":"THE LONG WAY's","odate":"1998-09-02","ocheck":"1","dogpic":""} 

Again notice the stored date of 1998-09-02 is put into json 1998-09-02.

So json_encode I double checked.

Don’t worry about the

$pages->getLimit2(), $pages->getPerpage() //part

Just custom code of mine.

Problem solved, see:

I also created an issue: #13170

I have had this problem once before with cakephp, I would store a 1 or a 0 in a tinyint field, but they decided to have the orm and query builder return “nothing” if a 0 is stored.

I thought ok, I will just handle it in code. I just found out about the date as well because of your question.

But you can strip off the part not needed with some php string functions.


This will work:

public function itest()
        $this->autoRender = false;
        $petid = 9;
        $pet = TableRegistry::getTableLocator()->get('Pets');
            $data = $pet
                    ->where(['petid' => $petid])
            $testdate = new \DateTime($data->odate);
            $newdate = $testdate->format('Y-m-d');
            $json = json_encode($newdate);
            echo $json;

Returns correctly:


Spent 2 to 3 hours solving this, and shouldn’t have to, I wish Cakephp returned the correct data.

To completely change this to return the format stored, in config/bootstrap.php





In other versions it might be:


instead of


If Type , use it, if TypeFactory use that.

Tested and works.

I wish the core team had big red warning when there’s data that the orm returns in a chosen format that isn’t what the user actually stored.

Laravel, yii, doctrine, other php frameworks, linq, jdbc, native mysql, sql server, etc all return 1998-09-02 if that is what I stored. Just curious why the cake team chose not to.

@njuejohn To achieve what you want just set the json encoding format for I18n\Date and I18n\FrozenDate classes by adding the following statements to your bootstrap file:


Thankful to all. ADmad Solution of adding


in bootstrap file did the trick.