简体   繁体   中英

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);
    }

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())

See on DB Fidle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM