简体   繁体   中英

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.

I will perform following actions in the query :

  • sort the tables with date, ASC or 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM