Looks like I am having a case of Monday morning!!!
Setup
As you can my local machine is in Eastern time zone with Day light Saving is in effect. That can be seen from 'date' command below.
date ; php -r 'echo mktime() .PHP_EOL ;'
Mon Apr 18 11:14:29 EDT 2016
1460992469
I then generated a unix timestamp using php. It is suppose to give your current time and convert that to Unix epoch at UTC 0:0:0 on Jan 1 1970.
My mysql Session is set to UTC, which I imagine 1460992469 represent as it is converted to UTC by mktime.
The Problem
The trouble is the conversion back to est does not recognizes Daylight Saving. Can anyone help to point the flaw in my logic.
SELECT CONVERT_TZ(FROM_UNIXTIME(1460992469), ##session.time_zone ,'EST') as converted_to_est , FROM_UNIXTIME(1460992469) , ##session.time_zone;
+---------------------+---------------------------+---------------------+
| converted_to_est | FROM_UNIXTIME(1460992469) | ##session.time_zone |
+---------------------+---------------------------+---------------------+
| 2016-04-18 10:14:29 | 2016-04-18 15:14:29 | UTC |
+---------------------+---------------------------+---------------------+
I tried using 'EDT' in CONVERT_TZ to no avail already .
Don't use 'EST'. Use 'America/New_York' (assuming United States).
Related
This question already has answers here:
Convert Javascript time to MySQL format using PHP
(4 answers)
Closed 8 years ago.
I have a series of dates stored in my database from an import. They're in the format of
Tue 2 Sep 2014
What would be the best way to convert this to MySQL date format, for example, yyyy-mm-dd hh:mm:ss either using PHP or MySQL.
Thanks
It's easy, just use date() combined with strtotime()
$date = "Tue 2 Sep 2014";
$conv = date("Y-m-d H:i:s", strtotime($date));
echo $conv;
output:
2014-09-02 00:00:00
To omit the time just remove H:i:s.
You can use str_to_date function to convert a string to a date as
mysql> select str_to_date('Tue 2 Sep 2014','%a %e %b %Y') as date ;
+------------+
| date |
+------------+
| 2014-09-02 |
+------------+
You can also format the date as
mysql> select date_format(str_to_date('Tue 2 Sep 2014','%a %e %b %Y'),'%Y-%m-%d %H:%i:%s') as date ;
+---------------------+
| date |
+---------------------+
| 2014-09-02 00:00:00 |
+---------------------+
Check more about date_format and other date functions here
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
How should I store my "dates + times" in a PostgreSQL database?
This is what I want to achieve:
How can I have all the entries that occurred on (for example) 1 January 2012 00:00:00 local time anywhere in the world?
Display all the entries sorted by date according to UTC time. (2012 New Year Eve in New York is more recent than the New Year in London).
How should I store my data? I have read that PostgreSQL stores all time in UTC internally (PostgreSQL documentation), so my users timezone is in fact lost.
I think I should use one column with type "timestamp without timezone":
Point 1 is easy.
And with another column of type "String" I will store the timezone string (e.g : America/New_York)
but then, point 2 seems still hard to do ....
I hope I am clear.
Edit new idea: I think with storing two timestamps: one without timezone (1. ok) and one with timezone (2. ok)
Yes, PostgreSQL stores all timestamps as UTC internally. For a timestamp with time zone the time zone offset is only applied to adjust the time to UTC, but is not stored explicitly.
I would not store the timezone string or even less a time zone abbreviation (those are not precise). This can later require expensive computation, because you have to consider daylight savings time and other oddities of the international time regime.
You can either store the time zone offset as interval (takes 12 bytes) or a numerical amount of seconds (takes 4 bytes as integer) like I demonstrate in this related answer.
Or, like you already proposed: store the local timestamp in addition to the UTC timestamp (takes 8 bytes). That would make your tasks easy. Consider the following demo::
-- DROP TABLE tbl;
CREATE TEMP TABLE tbl (id int, ts_tz timestamp with time zone, ts timestamp);
INSERT INTO tbl VALUES
(1,'2012-1-1 00:00+01','2012-1-1 00:00+01')
,(2,'2012-1-1 00:00+02','2012-1-1 00:00+02')
,(3,'2012-1-1 00:01+03','2012-1-1 00:01+03')
,(4,'2012-1-1 00:02+04','2012-1-1 00:02+04');
Query for question 1:
SELECT *
FROM tbl
WHERE ts = '2012-1-1 00:00'::timestamp;
id | ts_tz | ts
----+------------------------+---------------------
1 | 2012-01-01 00:00:00+01 | 2012-01-01 00:00:00
2 | 2011-12-31 23:00:00+01 | 2012-01-01 00:00:00
Query for question 2:
SELECT *
FROM tbl
ORDER BY ts_tz;
id | ts_tz | ts
----+------------------------+---------------------
4 | 2011-12-31 21:02:00+01 | 2012-01-01 00:02:00
3 | 2011-12-31 22:01:00+01 | 2012-01-01 00:01:00
2 | 2011-12-31 23:00:00+01 | 2012-01-01 00:00:00
1 | 2012-01-01 00:00:00+01 | 2012-01-01 00:00:00
The tricky part with this solution may be to enter the local timestamp. That's easy as long as all data is entered locally. But it needs consideration if you enter data for, say, New York in Los Angeles. Use the AT TIME ZONE construct for that:
SELECT ('2012-1-1 00:00+00' AT TIME ZONE 'America/New_York')::timestamp
, ('2012-1-1 00:00+00' AT TIME ZONE 'America/Los_Angeles')::timestamp
timezone | timezone
---------------------+---------------------
2011-12-31 19:00:00 | 2011-12-31 16:00:00
Note how I use a timestamp with time zone as input. AT TIME ZONE gives different results for timestamps with or without time zone.
I guess you could store the date as a string and use ISO 8601 or RFC 2822 so you also store the timezone.
Or indeed for more comparison options store timestamp and timezone in 2 different columns.
PHP date_default_timezone_set() from GMT offset is possible?
im mySQL db I have:
|timezoneid | gmt_offset | dst_offset | timezone_code | zone_name
| 1 | -12 | 0 | NULL | (GMT-12:00) International Date Line West
I want to be able to set time zones from database
Check the output of DateTimeZone::listAbbreviations(), it gives you a full list of supported timezones and corresponding offsets. You should check the exact output yourself, since you get nested arrays.
Also note that not all timezones have an exact full hour offset to UTC, some regions add 30 or 45 minutes too.
I have a php script which gives server time, but as per knowledge, we can get the user's machine time and time zone with the help of client side scripts (javascript). I have mysql table which has dates stored along with timezone from where it was stored with the help of form. I need to show the data on a web page along with the time stored in the table, but it should be per user's machine.
rowid | date | timezone | data |
----------------------------------
12 | 2010-07-13 12:30:00 | Asia/Kolkata | This is data field |
143 | 2010-07-13 12:30:00 | Europe/Prague | This is data field |
Now, when I show date on web page, I need to show the above date per user's machine timezone.
A workaround logic would be that, if I could pass the date (2010-07-13 12:30:00) to some JS method which will take two time zone as input and convert the given date into user's machine's timezone. Or Something else?
Please help?
See the following link:
http://us3.php.net/manual/en/function.date-timezone-set.php
You can set the timezone in the script before displaying the dates although make sure you insert all the times in the DB as GMT.
I'm trying to enter a date in a mysql table
`ssdate` datetime
The function that produces the date can output it in 3 formats:
1276142400000
Thu Jun 10 00:00:00 GMT-0400 2010
Fri Jun 4 2010
I'm wondering which of these would be easiest to convert to this field format?
Since I'm trying to save only the date, is there a better option than datetime that would work with one of these output formats?
You can use the third format:
date('Y-m-d H:i:s', strtotime('Fri Jun 4 2010'));
Just put the result in your datetime field. If you're going to use a date field instead you can do
date('Y-m-d', strtotime('Fri Jun 4 2010'));
The easiest way would probably be FROM_UNIXTIME(), but 1276142400000 does not appear to be a Unix timestamp:
mysql> SELECT FROM_UNIXTIME(1276142400000);
+------------------------------+
| FROM_UNIXTIME(1276142400000) |
+------------------------------+
| NULL |
+------------------------------+
1 row in set (0.00 sec)
Perhaps it's a Unix timestamp mutiplied by 1000:
mysql> SELECT FROM_UNIXTIME(1276142400000/1000);
+-----------------------------------+
| FROM_UNIXTIME(1276142400000/1000) |
+-----------------------------------+
| 2010-06-10 06:00:00 |
+-----------------------------------+
1 row in set (0.05 sec)
And, if unsure, you always have STR_TO_DATE():
mysql> SELECT STR_TO_DATE('Fri Jun 4 2010', '%a %b %e %Y');
+----------------------------------------------+
| STR_TO_DATE('Fri Jun 4 2010', '%a %b %e %Y') |
+----------------------------------------------+
| 2010-06-04 |
+----------------------------------------------+
1 row in set (0.00 sec)
You can set your column as a varchar(14), will work perfectly with your first format output.
In database I would store time values in in DATETIME field, mainly because of built-in methods for date manipulations (INTERVAL, etc.). One possible alternative is to store UNIX timestamp as numeric value, but I wouldn't recommend that. If in doubt, choose standard solutions.
As for date format to convert from, I would definitely go with UNIX timestamp (#1 option in your case, multiplied by 1000 I guess) as most universal one. All other formats are locale- and timezone-dependent, which is a possible source for a lots of headaches in the future.