Imagine that I have this table:
Id | Name | Timestamp ________________________________________________ 1 | Bill Gates | 2017-01-10 12:00:00 ________________________________________________ 2 | Mark Zuckerberg | 2017-07-23 17:00:00
If I use a simple query during the winter time like this:
SELECT Timestamp FROM TableName WHERE name="Bill Gates"
and try to print it using PHP or any other language I will get this output "2017-01-10 12:00:00".
If I use the exact same query but during the summer time (DST) I get this output "2017-01-10 13:00:00"
The Question: Is there any way to select the timestamp using the correct DST for each row based on the timestamp?
For example, if I select both rows from the table above, I will get the timestamp from line 1 as winter time and the timestamp from the second line as summer time. Can this be done with SQL only?
Thanks.
Best Answer
I'm not completely sure on your usage goal here. But the first paragraph on this link may be what your're looking for.
12.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME
Clarification on timestamp
In short with the exact table you have. No you can't.
The way time stamp works, is lets say you insert 5:00PM local time into your table. The very first thing MySQL does with a timestamp field is it converts that Local time it received to UTC. (eg. I'm in CST. so it would subtract 5 from my time).
At this point, what time zone it was when it was entered is lost. Instead it recalculates what time to display when it is selected in the future based on the local time of the connection (or default time zone of server) at the time of query. This allows for the Select statement to see the time stamp in terms of Your current local time.
Answer
To accomplish what you desire, your table would also need to contain a column for storing Which time zone the timestamp was inserted FROM/WHEN, so that you could use that to format the timestamp data when it is retrieved later. (As far as I can tell, this formatting would have to be done with logic (select timestamp between dates kind of thing) based on where/when the timestamp was inserted)