1

I found this sample source code for C# from How to convert a Unix timestamp to DateTime and vice versa?:

DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0);

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);

TimeSpan span = (date - epoch);

double unixTime =span.TotalSeconds.Dump();

If I was to insert into a MYSQL table the value of unixTime, would this be equivalent of a MYSQL timestamp datatype value? I just want to confirm this.

3
  • Can you post an example of what unixTime looks like? I don't know offhand what Dump does. Commented Nov 29, 2011 at 21:54
  • Sorry, I think unixTime should be double unixTime =span.TotalSeconds; Commented Nov 29, 2011 at 22:03
  • I think you should also create the start of Epoch in UTC. Commented Nov 29, 2011 at 22:29

1 Answer 1

1

It depends on the version of MySQL. TIMESTAMP field format has been changed from unixtime to dd-mm-yyyy hh:mm:ss from 5.something. If you want to insert a unixtime into a timestamp field, you should use FROM_UNIXTIME() builtin.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This is helpful but the question becomes could I pass to MYSQL's FROM_UNIXTIME(the value of unixTime) which is from the C# example above? I just want ensure my logic is correct.
Should work from what I understand. Isn't it easier to just test? Insert a date and then do select from_unixtime(field) from table_you_inserted_it_to; ? :)

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.