12

In my table I have a varchar column called date containing string representations of dates in dd/mm/yyyy format. How can I convert these to Unix times in a SELECT query?

1
  • You probably shouldn't be storing dates as dd/mm/yyyy strings in the first place. MySQL has a DATE type for storing dates; you should always use that to represent dates unless you have a good reason to do otherwise. If you were doing that, then you'd be able to get your Unix timestamp by just doing SELECT UNIX_TIMESTAMP(date) FROM yourtable. Since you're not storing your dates as dates in the first place, though, there's an extra conversion step (shown in the accepted answer) which makes things just a little more complicated. Commented Nov 28, 2015 at 19:30

3 Answers 3

25
select unix_timestamp(str_to_date('30/05/2011','%d/%m/%Y'));

or:

select unix_timestamp(str_to_date(myfield,'%d/%m/%Y')) from mytable;
Sign up to request clarification or add additional context in comments.

Comments

2

I think UNIX_TIMESTAMP should do the trick. Can you specify your select query here?

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_unix-timestamp

1 Comment

Here is what I did select unix_timestamp(str_to_date('date','%d/%m/%Y')) from tbl; Where date is column name. And the result is null
0

just use unix_timestamp function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.