Inércia Sensorial

18 de July de 2006

Required date and optional time fields, how to store it?

Filed under: Perils of Software Development — inerte @ 10:42

Say you have a form where your user can input a required date (MM-DD-YYYY) and an optional time (HH:MM). How do you store this?

You can be tempted to store it as a timestamp (seconds since the Unix Epoch, for example) on your database, because it represents a date + time. But if your user doesn’t fill the time field, you would need to store the time as 00:00:00, and deal with accordingly on your business/display logic.

This is bad, because some day, someone will want to record a time exactly at midnight, and you won’t be able to deal with correctly. Have two columns on your database, one for the date, and another one for the time (and make it NULL, to store the empty hours/minutes).

Another chapter of Perils of Development brought to you by Julio Nobrega (yes, this has happened to me).

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress