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

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).

Post a Comment

Your email is never shared.