# Custom Date Field in Database

**URL:** <https://discourse.cakephp.org/t/custom-date-field-in-database/11047>\
**Category:** Need Help\
**Created:** [March 13, 2023, 11:03am UTC](https://discourse.cakephp.org/t/custom-date-field-in-database/11047 "2023-03-13T11:03:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDebus](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@MDebus](https://discourse.cakephp.org/u/MDebus)\
**Post date:** [March 13, 2023, 11:03am UTC](https://discourse.cakephp.org/t/custom-date-field-in-database/11047/1 "2023-03-13T11:03:18Z")

</div>

Hey,

i have a database where unfortunately the date information is stored as an integer, for example 20231231 for December 31, 2023.  
Please don’t ask why this was done, but this is how the database was provided to me.

What is the best practice for converting this format to a more sensible one in CakePHP 4?  
I started writing my own DateType, extending the BaseType and implementing my conversion in the toPHP() and toDatabase() methods.  
However, now the date fields are not being displayed as “datetime-local” type in the form helper.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 13, 2023, 11:41am UTC](https://discourse.cakephp.org/t/custom-date-field-in-database/11047/2 "2023-03-13T11:41:36Z")

</div>

Just creating a custom DateType won’t work because you need to

- add that type to the `TypeFactory` and
- associate your columns to that new type

See [Database Basics - 4.x](https://book.cakephp.org/4/en/orm/database-basics.html#connecting-custom-datatypes-to-schema-reflection-and-generation)

---

<div class="post-metadata">

**Author:** ![MDebus](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@MDebus](https://discourse.cakephp.org/u/MDebus)\
**Post date:** [March 13, 2023, 12:55pm UTC](https://discourse.cakephp.org/t/custom-date-field-in-database/11047/3 "2023-03-13T12:55:27Z")

</div>

That is what i did.

// In my bootstrap.php  
TypeFactory::map(‘mydate’, ‘App\Database\Type\DateType’);

// In my Table  
$schema-\>setColumnType($column, ‘mydate’);

okay, that is the right way… can i tell the form-helper that it use automatically that type or do I always have to specify the type here?  
$this-\>Form-\>control(‘published’, [‘type’ =\> ‘datetime’]);

Danke 😉
