30

I have tried to insert date and time string formatted into mysql timestamp field by using following two methods but both shows me 0000-00-00 00:00:00

INSERT INTO test VALUES ( UNIX_TIMESTAMP('2013-08-05 18:19:03') )
INSERT INTO test VALUES ( UNIX_TIMESTAMP(STR_TO_DATE('2013-08-05 18:19:03', '%Y-%m-%d %H:%i:%s')) )

I believe first one should work as I am expecting but not sure why isn't parsing date and time?

3
  • There's nothing wrong with your UNIX_TIMESTAMP usage, as you can see by doing SELECT <your unix timestamp stuff> under mysql. Does your table have more than one column? Or perhaps the data type for the column is something other than int (because that's what UNIX_TIMESTAMP returns)? Aug 6, 2013 at 23:48
  • Try entering your INSERT statements into mysql and see how it responds. Aug 6, 2013 at 23:54
  • @GreatBigBore My field is timestamp, it's not a date and time column and there is only one column
    – Maximus
    Aug 7, 2013 at 0:16

1 Answer 1

38

The problem is that your field is defined as TIMESTAMP but UNIX_TIMESTAMP returns an int. Use INSERT INTO test VALUES ('2013-08-05 18:19:03' ) instead.

2
  • That worked! Thanks. I am wondering why strtotime php functions works fine but not UNIX_TIMESTAMP in mysql query? When I pass unix_timestamp value in date function it shows me 43 years back date.
    – Maximus
    Aug 7, 2013 at 0:15
  • It's probably because the PHP functions you're using are expecting something other than the int that is returned by UNIX_TIMESTAMP. Check the documentation carefully and you'll probably find the problem. Aug 7, 2013 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.