How to create TIMESTAMPS for SQL query for it to be sortable by date from PHP?
So I have table created by such SQL script
CREATE TABLE `Keys` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`KEY` INT NOT NULL ,
`TIMESTAMP` DATE NOT NULL ,
UNIQUE (
`KEY`
)
)
What sript should I write to:
- Add timestamps
- To sort\search thru that timestamps
TIMESTAMPin MySQL will be returned to PHP as strings, in theYYYY-MM-DD HH:MM:SSformat.Which means you’ll be able to use
strtotimeon them, to get an UNIX Timestamp, which is the "standard type" on which PHP works for dates/times.You’ll jsut have to consider that a timestamp is a number of seconds since
1970-01-01Note there is a limit on [timestamps][3] *(quoting)* :
And that limit is generally the same on the PHP side.
Then, to manipulate those timestamps, on the SQL side, you can use a wide range of [Date and Time Functions][4].
And they can be sorted like any other data-type — you can also use the standard
=,>,<, … operators.