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.