我有一个表单,要求用户提供开始日期和时间,有时不需要时间,因此我将字段分为 start_date
和 start_time
,其中start_date
是 date 类型,start_time
是 varchar,但我觉得 time> 字段更合适。
那么,就灵 active 和 future 连接能力而言,存储这些字段的最佳方式是什么?
或者使用单个字段并组合用户给出的两个值是更标准的做法吗?
最佳答案
and start_timewas a varchar but I feel that a time field is more appropriate.
是的,绝对是。一旦您需要在任何时候对值执行某种“时间操作”,例如添加或减去一定的分钟数,VARCHAR 就会变得很麻烦。
Or is it a more standard practise to have a single field and combine the two values given by the user?
由于时间是可选的,我认为您应该单独存储它 - 否则,您如何区分没有时间的记录和恰好有时间的记录00:00:00
在完整日期时间的时间部分?您可以将单独的时间列设为 NULL,但不能将日期时间值的“部分”设为 NULL。
关于php - 在 MySql 中存储日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58710162/