0

(After sending AJAX / $http POST request from AngularJS to PHP) I am trying to save unix timestamp (as INT) in my database, but it doesn't work.

It comes from JavaScript in this form: (new Date()).valueOf(), producing a value in my object: time_stamp : 1513079761979. When it arrives in my PHP I can echo it (as JSON, for debugging) and see it as ... : 1513079761979.

So far the value has not been altered. However, when I INSERT it into my database along with some other values, which work fine, including integers, it saves it as 2147483647, which is obviously is no longer the same value. There are now 10 digits so I thought the length was too short, but nope.

To fix it, I tried all sorts of methods: casting (int) in PHP, changing type of attribute to int(32) (bigger length) in my database. Nothing works.

The only solution I see is to save my integer as string (VARCHAR or length 1024~), which does work, but I want to know why it fails to accept INT as is, because I have other variables that I'm saving as integers, so I want to guarantee that those work as expected too.

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
  • 3
    `Int` will accept only 9 digit number, if you want to insert more than 9 digit number then please use `Big Int`. – Ayyappa amara Dec 12 '17 at 12:15
  • @Ayyappaamara why is the INT type selected as `int(11)` by default? does it still limit to 9 digits? – Aleksey Solovey Dec 12 '17 at 12:16
  • read [this post](https://stackoverflow.com/questions/5634104/what-is-the-size-of-column-of-int11-in-mysql-in-bytes) for reference. – Himanshu Upadhyay Dec 12 '17 at 12:17
  • https://dev.mysql.com/doc/refman/5.7/en/integer-types.html you will find your `2147483647` number on this page. Also use Unsigned if you don't expect negative numbers to increse your storage range. – Scuzzy Dec 12 '17 at 12:18
  • 2
    @HimanshuUpadhyay , @Scuzzy thank you for those links, that cleared it for me, also the issue was solved with `BIGINT` – Aleksey Solovey Dec 12 '17 at 12:20
  • also, it may be worth converting your timestamps to date objects for MySQL, you may get some benefit from the date functions when searching/returning – Scuzzy Dec 12 '17 at 12:22
  • @AlekseySolovey, happy to help. Happy coding. – Himanshu Upadhyay Dec 12 '17 at 12:23

0 Answers0