This is a conceptual question, so no code snippets here.
Say I create a database of events. Some of them are in New York, some in Chicago, some in Phoenix, etc...
My server's timezone is set to New York.
In my mind, I have two options when creating UNIX timestamps for all these events.
Take the timezone into account. (i.e., An event at midnight on January 1 in Chicago and Pheonix would have different timestamps). Then I'd have to take the timezone into account again whenever I want to display the date in text format.
Fudge it by pretending that all events happen in New York. An event at midnight on January 1 in Chicago and Pheonix would have the same timestamp. Since my server is set to New York, I wouldn't have to take the timezone into account for each event.
Which approach is better? Approach 1 gives a more "true" timestamp, but approach 2 seems less complex while still giving the same result.
If you mean that you are recording the time of events as they happen, or recording the times of events in the past, then paxdiablo's answer is correct. Usually, UTC will suffice for that. In a few cases, you might want to store a local datetime + offset (a "DateTimeOffset" in some platforms), but that depends on exactly what you're using the data for.
However, if you are asking about scheduling an event that will happen in the future, especially if it's a recurring event, then UTC is not entirely sufficient. I've already written about this several times, so I suggest you read these articles for details:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With