I would like some confirmation on how NOW function works in MySQL
According to the docs and when runing the this query SELECT NOW() the mysql returns the current time (local time i guess?) in following format YYYY-MM-DD HH-MM-SS.
If this is the case, then how come this works when comparing NOW() to a column that includes a UTC ISO date and time?
For example this works fine:
SELECT * FROM table where deadline > NOW() # deadline column contains a utc ISO string
Is the query above reliable or did it just return the correct answer by luck?
in case this is NOT reliable, how would you do the comparison?
MySQL NOW()-function returns a datetime in the session timezone. MySQL has UTC_TIMESTAMP()-function which returns current UTC date and time, which will work better when you are compare it to an UTC date time.
Note that you should store datetimes as DATETIME, instead of char/varchar (assume this is what you meant by "UTC ISO date and time").
Related
I'm storing date time in 04-09-2019 10:31:AM(4 September 2019 10:31 AM) in this format in MySQL table.
May I know how to convert this format to unixtime stamp in sql query
First you need to convert this into a proper mysql datetime using str_to_date() function.
str_to_date(dd, '%d-%m-%Y %h:%i:%p')
Then after getting the correct datetime value, use unix_timestamp() function to convert it to unix timestamp.
select
unix_timestamp(str_to_date(dd, '%d-%m-%Y %h:%i:%p'))
from test
try this dbfiddle.
I have two unix timestamps 65 seconds apart and i am trying to query mysql in this manner
This is 65 seconds apart 1504684252 + 65
SELECT ask FROM live_rates WHERE the_time BETWEEN
UNIX_TIMESTAMP(1504684252) AND UNIX_TIMESTAMP(1504684317)
SELECT ask FROM live_rates WHERE the_time BETWEEN
FROM_UNIXTIME(1504684252) AND FROM_UNIXTIME(1504684317)
In my table, there is an event starting at timestamp 1504684252 and ending 65 seconds later. Why is there no data returned by either of the queries?.
Your question doesn't really make it clear what data type the_time is - if it's a unix timestamp (i.e. an integer) already, just query it as an integer, given that you seem to know the unix timestamps of your date range:
SELECT ask FROM live_rates WHERE the_time BETWEEN 1504684252 AND 1504684317
If your data in the_time is stored as a datetime, I'd say you should make sure that the server really is converting the values you've supplied to a datetime range that includes the relevant record:
SELECT ask FROM live_rates WHERE id = abcdef
SELECT FROM_UNIXTIME(1504684252), FROM_UNIXTIME(1504684317)
Run these two queries (with the id replaced) and eyeball the data. You'll find the reason, I'm sure - possibly something like a timezone issue, and your unix timestamps are translating to some time in UTC whereas your table data is showing some other time and you've hence mis-translated the time shown into a unix time.. Or possibly that it's an end date that has some time component outside the range of the unix times you specified even though the date part of it is correct.
It's hard to say for sure without a complete, verifiable example (a create table statement with inserted test data and a select demonstrating the problem. Try sqlfiddle.com)
ps; Don't use UNIX_TIMESTAMP() in the way you wrote here; it's not intended to have a unix timestamp passed into it. You either pass it no arguments (in which case it gives you the current datetime of the server clock, as a unix timestamp) or you pass it some datetime (and it will return you the equivalent unix timestamp of that datetime)
If the_time is a unix timestamp as opposed to a date i.e. 1504684252 instead of 2017-09-01 00:00:00, then you don't need FROM_UNIXTIME, which converts a unix timestamp to a date:
SELECT ask FROM live_rates WHERE the_time BETWEEN 1504684252 AND 1504684317
UNIX_TIMESTAMP takes a date/time as parameter and returns an integer. When you're feeding it with an integer it returns 0 - so you're selecting between 0 and 0.
FROM_UNIXTIME takes an integer but it returns a date/time - so if the_time is an integer they're not compatible.
I've a timestamp field in my database in which data is inserted from python datetime module. I want to convert the timestamp to date and do some filtering. I can do this in python but instead I want to do this in sql query itself.
I tried this so far
Select DATE_FORMAT(FROM_UNIXTIME('ts'), '%Y %b %e') AS 'date_formatted' from tablename limit 100;
This is converting to date but the from_unixtime is messing it to dates in 1969 instead of 2013.
I need to format python's timestamp in mysql.
Is ts the column name of the timestamp? If so, why are you putting it in single quotes? That makes it into a literal string.
MySQL date functions translate a literal string that isn't a timestamp, silently, into NULL. A NULL or zero UNIX timestamp refers to the beginning of the UNIX epoch, which is 1-Jan-1970 at midnight universal time. That's 19:00 on 31-Dec-1969 New York time. That is, I believe, where your 1969 stuff is coming from.
I have a blog where users can comment. I insert the time at which they posted a comment using NOW() and then use date('j M Y', stored timestamp) to show the time at which they posted.
I want to know does NOW() return the locatime of the end user or the localtime at my server.
Is it better suited to use UNIX_TIMESTAMP than NOW() to calculate the localtime at which users posted a comment.
The function NOW() generates a formatted date-time string, determined by the time zone of your MySQL server.
However, it would be better to store times using UNIX_TIMESTAMP(), which is expressed in GMT. Doing so makes it easier to format it according to the country of a visitor (e.g. using JavaScript).
If you still want to use DATETIME columns, you can store times using UTC_TIMESTAMP() (it formats a date like NOW() but expresses it in UTC); it should more or less work the same in all other aspects.
MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer if no arguments are passed with UNIT_TIMESTAMP().
When this function used with date argument, it returns the value of the argument as an unsigned integer in seconds since '1970-01-01 00:00:00' UTC.
Argument may be a DATE, DATETIME,TIMESTAMP or a number in YYYYMMDD or YYMMDD.
Note : Since UNIX_TIMESTAMP() works on current datetime, your output may vary from the output shown.
NOW() returns the current date and time.
SELECT NOW(), UNIX_TIMESTAMP(NOW());
+---------------------+-----------------------+
| NOW() | UNIX_TIMESTAMP(NOW()) |
+---------------------+-----------------------+
| 2011-10-03 10:22:37 | 1317666157 |
+---------------------+-----------------------+
Let's see what the manual has to say about NOW():
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS'
or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is
used in a string or numeric context. The value is expressed in the
current time zone.
... and UNIX_TIMESTAMP():
If called with no argument, returns a Unix timestamp (seconds since
'1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP()
is called with a date argument, it returns the value of the argument
as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE string,
a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or
YYYYMMDD. The server interprets date as a value in the current time
zone and converts it to an internal value in UTC.
So, to begin with, they return different things: a proper date versus an integer.
You actually need to get three features:
Store all dates in the same format (either UTC or the server's time zone)
Obtain user's time zone
Display stored date in user's time zone
The Date and Time functions chapter offers a summary of available functions. If you want to store dates in UTC you'd go for UTC_TIMESTAMP(). If you want to use server's time zone you can use NOW(). And there's CONVERT_TZ() to make conversions.
MySQL, however, won't help you with point #2. You need to either ask the user or use JavaScript to read user's clock and send it to the server so you can guess (if you don't ask you'll always need to guess because there're normally several time zones that share the same time in a given instant).
I need to pull a variable amount of days of data from a MySQL database that stores the date as a UNIX timestamp in an int column.
For example, if I need the last 5 days of data, what would my query be?
(All queries would start from current date and would go back x amount of days).
Timestamp is considered one of the Date and Time types and therefore any of the Date Time Functions can be used on it.
SELECT * FROM your_table
WHERE Ftimestamp_column > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 DAY));
I've never tried it but there's a MySQL function to convert unix timestamps into MySQL dates and then you can use DATE_SUB() or whatever. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime