2
mysql> describe taps;
+-------------+-------------+------+-----+-------------------+-------+
| Field       | Type        | Null | Key | Default           | Extra |
+-------------+-------------+------+-----+-------------------+-------+
| tag_id      | int(11)     | NO   |     | NULL              |       |
| time_stamp  | timestamp   | NO   |     | CURRENT_TIMESTAMP |       |
| device_id   | varchar(45) | YES  |     | NULL              |       |
| device_type | varchar(45) | YES  |     | NULL              |       |
+-------------+-------------+------+-----+-------------------+-------+

mysql> INSERT INTO `taps` (tag_id, time_stamp) VALUES(0, 1451610061);
ERROR 1292 (22007): Incorrect datetime value: '1451610061' for column 'time_stamp' at row 1

WHY?? I have found many similar questions, but not of them seem quite this black and white.

1451610061 is a valid timestamp. I checked it at http://www.unixtimestamp.com/ and it evaluates as expected.

So, why doesn't MySql like it?

1
  • 1
    why you use 1451610061 directly to your query? try to format it first as the field created already. Feb 13, 2016 at 15:57

2 Answers 2

5

The MySQL timestamp format is 2016-02-13 15:48:29 or Y-m-d H:i:s convert your timestamp to that format first, and then MySQL will accept it.

If you insert a new record without defining the timestamp, and then select the row from that table, you will notice that that's the format that it gives to the new default record.

if if you want to convert it directly in your query use:

INSERT INTO `taps` (tag_id, time_stamp) VALUES(0, from_unixtime('1451610061'));

Using this Q&A on StackOverflow as reference. And from_unixtime documentation

1
  • 1
    D;oh! I had momentarily thought that it was an integer representing the number of seconds from the epoch. Very tired after a very long coding session. Thanks for being awake :-) Feb 13, 2016 at 18:23
1

Try this ->

$item->time_stamp = date('Y-m-d H:i:s', strtotime($request->time_stamp));

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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