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?

Try

$row->your_field->toDateTimeString(); 

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.

EDIT:

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.

Edit

This will work:

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

Returns correctly:

"1998-09-02"

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

change

TypeFactory::build('time')
    ->useImmutable();
TypeFactory::build('date')
    ->useImmutable();
TypeFactory::build('datetime')
    ->useImmutable();
TypeFactory::build('timestamp')
    ->useImmutable();

To:

TypeFactory::build('time')
        ->useImmutable();
TypeFactory::build('date')
        ->useImmutable()
        ->useLocaleParser()
        ->setLocaleFormat("yyyy-MM-dd");
TypeFactory::build('datetime')
        ->useImmutable();
TypeFactory::build('timestamp')
        ->useImmutable();

In other versions it might be:

Type::build('date')

instead of

TypeFactory::build('date')

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, asp.net 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:

\Cake\I18n\Date::setJsonEncodeFormat('yyyy-MM-dd'); 
\Cake\I18n\FrozenDate::setJsonEncodeFormat('yyyy-MM-dd');  

Thankful to all. ADmad Solution of adding

\Cake\I18n\Date::setJsonEncodeFormat(‘yyyy-MM-dd’);
\Cake\I18n\FrozenDate::setJsonEncodeFormat(‘yyyy-MM-dd’);

in bootstrap file did the trick.