Convert MySQL date time 24 hour format to UNIX timestamp - mysql

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.

Related

MySQL query comparing NOW() with ISO string

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").

How to convert this date into a unix timestamp in mysql alone?

This is the date: 2018-01-25T13:39:40-05:00
Its from an API.
So far, I did this: SELECT UNIX_TIMESTAMP(STR_TO_DATE('2018-01-25T13:39:40-05:00', '%M %d %Y %h:%i%p')) AS time
But its giving me NULL.
I think -05:00 is timezone. But T is, I don't know.
Maybe perhaps there is another way take out T and -05:00 and convert 24hrs to 12hrs
You don't have to use the STR_TO_DATE function.
You can directly use UNIX_TIMESTAMP.
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.
The date argument may be a DATE, DATETIME, or TIMESTAMP string, or a number in YYMMDD, YYMMDDHHMMSS, YYYYMMDD, or YYYYMMDDHHMMSS format.
The server interprets date as a value in the current time zone and converts it to an internal value in UTC. Clients can set their time zone
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
SELECT UNIX_TIMESTAMP('2018-01-25T13:39:40-05:00') AS time
http://sqlfiddle.com/#!9/b2206f/1

SQL: Convert date (26.11.17) to timestamp (1516184035)

Is there any way to convert a normal european date to a timestamp-value in mySQL?
I have a mysql database with a column date (varchar(64)) containing dates in european format like 01.12.15. For this I need a update-sql-query that converts all lines into timestamps.
Thanks for your help!
Use STR_TO_DATE to convert your date string 26.11.17 to a bona fide date. Then call UNIX_TIMESTAMP on that date to get the UNIX timestamp.
SELECT
UNIX_TIMESTAMP(STR_TO_DATE('26.11.17', '%d.%m.%y')) AS output
FROM dual;
1511650800
Demo

Converting date/time string to unix timestamp in MySQL

We have a database with all the dates and times stored in one column called timestamp. The format of the date/time in the column "timestamp" is as: 03 Aug 08:10am.
I would like to convert this (03 Aug 08:10am) to UNIX TIMESTAMP in MySQL and not PHP because we already have over 500 rows with this format: 03 Aug 08:10am.
I tried create a new INT column called new_timestamp and ran this query:
UPDATE table_name SET new_timestamp = UNIX_TIMESTAMP(timestamp);
However, it shows that 0 rows were affected.
This is not a duplicate, don't redirect me to how to convert in PHP. Read the question first :)
The UNIX_TIMESTAMP() function requires a valid date/time format to convert correctly, so you need to convert your existing date/time format to a valid/recognised format (including the year) first. You can do this using MySQL's STR_TO_DATE() function, telling it what format you are passing in, and concatenating in a hard-coded year value as it's always 2016 in your case.
STR_TO_DATE(CONCAT('2016-', <your date/time value>), '%Y-%d %b %h:%i%p')
You can then use the UNIX_TIMESTAMP() function to convert that valid date to your unix timestamp and update all those records in a single step:
UPDATE table_name
SET new_timestamp =
UNIX_TIMESTAMP(STR_TO_DATE(CONCAT('2016-', timestamp), '%Y-%d %b %h:%i%p'));

How to convert python timestamp in mysql database to date in a SQL query

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.