I love the 70s, but that's just not the case (PHP Time question)

So, I save the call to the secret field () with every entry in my DB. The only problem is they all come out like:

16777215

which is from 1970. I cannot tell why this number is because it is not the beginning or end of the current timestamp and it is the same for every entry. It goes into the MySQL column of the MySQL environment (50) and the field looks like this:

    <input type="hidden" name="time" value="<?php echo time(); ?>">

      

Is this a casting problem? I bet it is. Sunnuva pistol. Let me check that ...

So, I changed the column to "Text" and it is entered accurately. So if I want it to be a number to do math with it, do I need to use php to convert it to int before I save it? And just for curiosity, why did he choose that number earlier?

+2


source to share


3 answers


If you are storing dates, I highly recommend using the built-in Date types in MySQL. What for? You can do date validation (MySQL won't let you enter month 13, for example), generating queries manually is much easier, and when looking at the raw data, you know what a date is. MySQL will also let you use things like CURRENT_TIMESTAMP () on columns that you need to record the time when the row changes.

Storing dates as integers makes simple math a lot easier to do, but I think this is about where the code ends up advantageous. PHP has a strong Date / Time library that you can use to work with dates. If you want to see the amount of time between two dates, you can use Date :: diff. You can also add and subtract times from dates. (Yes, there are functions that correspond to objects if you choose to use functions instead of objects.)



Manual Entry for Date / Time Objects

+4


source


16777215 is the maximum number for the environment. You need to do it int or Bigint. See MySQL Manual



+11


source


Why not use a datatype for storing dates?

+8


source







All Articles