简体   繁体   English

在mysqli数据库中存储日期和时间的最佳方法是什么?

[英]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 : 我将在查询中执行以下操作:

  • sort the tables with date, ASC or DSC 使用日期,ASC或DSC对表进行排序
  • sort the tables with respect to time, 根据时间对表格进行排序,
  • 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.

相关问题 在 MySQL 数据库中存储日期和时间以供以后使用 PHP 显示的最佳方法是什么? - What's the best way to store date and time in a MySQL database for later display with PHP? 在数据库中存储特殊字符的最佳方法是什么? - What is the best way to store special character in database? 在数据库中存储大量标记的最佳方法是什么? - what is the best way to store a lot of markers in database? 使用MySQL PHP + AJAX存储日期和时间的最佳方式 - Best way to store date and time with MySQL PHP + AJAX 将html数据存储在mysql数据库中的最佳方法是什么? - What's the Best way to store html data in a mysql database? 使用Codeigniter在数据库中存储站点配置/选项的最佳方法是什么? - What is the best way to store site configuration/option in database using Codeigniter? 在MySQL数据库中存储特殊字符的最佳方法是什么? - What is the best way to store special characters in a MySQL database? 在数据库中存储多个复选框值并在以后使用它的最佳方法是什么 - What is the best way to store multiple checkbox values in the database and work with it later on 在 MySQL 数据库中存储复选框值的最佳方法是什么? - What's the best way to store Checkbox Values in MySQL Database? 在mysql数据库中存储日期和时间的最准确方法 - the most accurate way to store date and time in mysql database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM