[英]What is the best way to store date and time in mysqli database?
What is the best way to store date and time into separate fields in MySQL database. 将日期和时间存储到MySQL数据库中单独字段中的最佳方法是什么。
I will perform following actions in the query : 我将在查询中执行以下操作:
select tables by certain conditions such as: 根据某些条件选择表,例如:
example query : 示例查询:
SELECT * FROM table WHERE starttime > '$starttime AND endtime < $endtime
lets say 可以说
$date = "11/12/2016";
$starttime = "09:00 AM";
$endtime = "05:00 PM";
what is the best way to store these in correct format? 以正确的格式存储这些文件的最佳方法是什么?
In our use cases we have to consider time zones, so a complete date is a combination of date,time and timezone. 在我们的用例中,我们必须考虑时区,因此完整日期是日期,时间和时区的组合。
Since date and time are related I prefer to translate everything to miliseconds since 1.1.1970 in UTC (epoch timestamp) and store these values. 由于日期和时间相关,因此我更喜欢将所有内容转换为以UTC(纪元时间戳记)为1.1.1970以来的毫秒数并存储这些值。 The advantage is an absolute time without the need to remember the timezone.
优点是绝对时间,无需记住时区。 Sorting, filtering, finding intersection of appointments is very easy.
排序,过滤,查找约会交集非常容易。
However the moment you translate the long value back to a human readable value you need the time zone for which the date was created. 但是,当您将long值转换回人类可读的值时,您需要创建日期的时区。 So you must keep this data somewhere.
因此,您必须将此数据保存在某处。 In general the timezone is the for one user so all date related to a user have the same timezone.
通常,时区是一个用户的时区,因此与一个用户相关的所有日期都具有相同的时区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.