jimw's picture

Storing dates in Drupal Schema API

Posted by:

This one caught me out for a good while – I’ve got my own data stored in a database using Drupal’s Schema API and one field I want is a date, so I used the ‘datetime’ type. But whenever I came to use a value anywhere I couldn’t get any of Drupal’s date formatting functions to work – they were expecting a unix timestamp (makes sense) but the Schema API uses ‘datetime’ as a field type on MySQL so was getting a MySQL date string in return.

The answer was not to use datetime as a schema type, but int, and when passing data make sure you pass a timestamp. format_data() and views_handler_field_date etc. will then work as expected.

But it makes you wonder who left this mantrap lying around for foolhardy developers like me to fall into?

Your email address will not be published.

Digital Agency Norwich

About you

Alternatively why not give us a call.
We are always happy to talk

We would like to hear from you so please feel free to fill out the following details and any questions you may have and we will get back to you asap

+44(0)1603 727744