'The best way to save the date in the database to work with it in
I have doubts regarding what type of data I have to save in the database if what I want in the future is to be able to list the records between dates.
For example: See how many records there are between 18 and 19 hours of X day. View the last 7 days. See how many records there are in the last 24 hours
That is to say, what type of data is better to be able to extract the records between the hours, days, months, minutes that I need.
I had thought about Datetime and save it by means of:
date('Y-m-d H:i:s', time());
I have this now and dont work
public function addRow($date, $user, $country, $city, $inspiration, $title, $link){
$consulta = "INSERT INTO table_hand (id, fecha, user, title, link, country, city, inspiration, views, hands) VALUES ('', SYSDATE(), '$user', '$title', '$link', '$country', '$city', $inspiration, 0, 0)";
$this->conexion_db->query($consulta);
}
Solution 1:[1]
I believe that the best way to save a date in the MySQL database would be to create a column of type DATETIME and, when inserting, pass the SYSDATE() or NOW() function to the field (or let this by column default value)
See an example:
Table DDL:
CREATE TABLE Date_Table (
`default_date` DATETIME
);
Insert:
INSERT INTO Date_Table
(`default_date`)
VALUES
(SYSDATE())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ergest Basha |
