Bilal Sardar
Bilal Sardar

Reputation: 115

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 :

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?

Upvotes: 0

Views: 862

Answers (1)

Frank Essenberger
Frank Essenberger

Reputation: 345

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.

Upvotes: 2

Related Questions