I now have a combobox - two (day, month) and a text field (year). I want to add this data to the database. Question: How are they put? Create three separate fields for them and enter data one by one or is there a simpler method?

    3 answers 3

    For example,

    $day = isset($_POST['day']) ? (int)$_POST['day'] : 0; $month = isset($_POST['month']) ? (int)$_POST['month'] : 0; $year = isset($_POST['year']) ? (int)$_POST['year'] : 0; /** * Если в UNIX формате (TIMESTAMP) * * тогда в БД дата хранится в целочисленном типе (integer) */ $timestamp = mktime(0, 0, 0, $month, $day, $year); /** * Если в формате DATE */ $date = date('Ym-d', mktime(0, 0, 0, $month, $day, $year)); 

    Next, add to the database.

    PS It is necessary to store in one field, because it is more convenient to perform sorting, and if you use MySQL native data types (DATE, DATETIME ...), then this makes it possible to perform various operations with the date directly in the request.

    • Then if I take the third case and write it there. So when you call from the database data, it will appear in the format ymd, right? - navi1893
    • one
      @ navi1893 What is the third? - romeo

    I would advise to keep the date of birth as it is more convenient for you in your situation. As for me, this is the best type of DATE , but do not forget that in MySQL there are still many data types that can suit you. I met how people store date data in an INT type.

    TIMESTAMP - data type for storing date and time. Data is stored in the form of the number of seconds elapsed since the beginning of the “Unix era”. Range of values: 1970-01-01 00:00:00 - 2038-12-31 00:00:00. It takes 4 bytes.

    YEAR is the data type for storing the year. Range of values: 1901 - 2155. It takes 1 byte.

    DATE is a data type for storing a date. Range of values: 1000-01-01 - 9999-12-31. It takes 3 bytes.

    TIME - data type for storing time. The range of values ​​is −828: 59: 59– 828: 59: 59. It takes 3 bytes.

    DATETIME is a data type for storing date and time. Range of values: 1000-01-01 00:00:00 - 9999-12-31 00:00:00. It takes 8 bytes.

      How do you want and save, at least in separate fields, at least in one ... It is better to store in one. When receiving dates from the user, collect them in one line ... When outputting, you can format them or just break them apart ... It depends on what you need.