CakePHP - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xE2\x80\x8B}

cakephp4 SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xE2\x80\x8B} CakePHP

This MySql or MariaDB error is generally due to character encoding, whereas a value is being saved to the database, that isn't encodeable with current encoding.

In my case, the field causing the error was encoded in latin_swedish_ci - default for the server.

 

Solution 1: Remove unknown UTF8 characters

Remove unrecognized UTF-8 characters (for my input, these are generally unrecognized spacing characters).
mb_convert_encoding($articleUpdate['content'], 'UTF-8', 'UTF-8');

Solution 2: Recommended based on research, but didn't work for me.  Solution 1 worked, but I had already completed the steps in Solution 2, so unsure if parts of #2 affected #1.

The solution is to change the encoding on the field to accommodate the unrecognized characters - I changed mine to the following, per recommendations from the research done.

utf8mb4_unicode_ci

In the app_local.php file, or app.php, add the following to your database connection array:

'encoding' => 'utf8',
'collation' => 'utf8mb4_unicode_ci',

 

As an extra measure, you can encode your string:

mb_convert_encoding(html_entity_decode($articleUpdate['content']), "UTF-8");

Share this Post