Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    controllare date impossibili con mysql

    sto usando mysql 4.0.24 ed ho un dubbio relativo all' inserimento di date .

    ho usato una tabella con campo date.

    effettuo inserimento di date nella forma YYYY-MM-DD.

    giustamente l' inserimento della data : 2006-24-01 restituisce un bel 000-00-00 in quanto il mese "24" non esiste .

    il dubbio riguarda l' inserimento di una data del tipo : 2006-02-31.

    perche' viene inserita (neanche warning) se il 31 febbraio e' una data inesistente ?
    ( \_/ )
    ( *.* )
    (> <) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  2. #2
    MySQL version through 4.1 accept certain “illegal” values for dates, such as '1999-11-31'. This is useful when you want to store a possibly incorrect value specified by a user (for example, in a web form) in the database for future processing. MySQL verifies only that the month is in the range from 0 to 12 and that the day is in the range from 0 to 31. These ranges are defined to include zero because MySQL allows you to store dates where the day or month and day are zero in a DATE or DATETIME column. This is extremely useful for applications that need to store a birthdate for which you do not know the exact date. In this case, you simply store the date as '1999-00-00' or '1999-01-00'. If you store dates such as these, you should not expect to get correct results for functions such as DATE_SUB() or DATE_ADD that require complete dates.

    MySQL also allows you to store '0000-00-00' as a “dummy date”. This is in some cases more convenient, and uses less data and index space, than storing NULL values.

  3. #3
    quindi mi confermi che o aggiorno mysql o devo controllare a priori la validita' della data ?
    ( \_/ )
    ( *.* )
    (> <) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  4. #4
    Originariamente inviato da honneamise
    quindi mi confermi che o aggiorno mysql o devo controllare a priori la validita' della data ?
    non lo so ma non credo che nella 5 vengano effettuati controlli di validità sulle date, credo gli garbi così.

    Fai i controlli in php prima di inviare i dati, c'è la funzione apposita:

    bool checkdate ( int month, int day, int year )

  5. #5
    grazie mille .
    ( \_/ )
    ( *.* )
    (> <) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  6. #6
    riuppo la discussione in quanto ho aggiornato la versione di mysql alla 4.1

    ho "dovuto" aggiornare in soldoni perche' alcune funzioni come datediff per esempio non erano implementate nella versione che stavo usando .

    ecco un paio di esempi chiarificatori .

    codice:
    mysql> select version();
    +---------------------------+
    | version()                 |
    +---------------------------+
    | 4.1.11-Debian_4sarge3-log |
    +---------------------------+
    1 row in set (0.00 sec)
    
    mysql> select DATE("2005-02-31");
    +--------------------+
    | DATE("2005-02-31") |
    +--------------------+
    | 2005-02-31         |
    +--------------------+
    1 row in set (0.00 sec)
    
    mysql> select DATE("2005-02-27");
    +--------------------+
    | DATE("2005-02-27") |
    +--------------------+
    | 2005-02-27         |
    +--------------------+
    1 row in set (0.01 sec)
    
    mysql> select DATE_ADD("2005-02-27",INTERVAL 1 DAY);
    +---------------------------------------+
    | DATE_ADD("2005-02-27",INTERVAL 1 DAY) |
    +---------------------------------------+
    | 2005-02-28                            |
    +---------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select DATE_ADD("2005-02-27",INTERVAL 2 DAY);
    +---------------------------------------+
    | DATE_ADD("2005-02-27",INTERVAL 2 DAY) |
    +---------------------------------------+
    | 2005-03-01                            |
    +---------------------------------------+
    1 row in set (0.00 sec)
    per quanto riguarda il controllo delle date (nel mio caso specifico ho bisogno di inserire delle date ed in seguito calcolare delle scadenze), prima di inserire una data nel db devo verificarla via php.

    in seguito uso INTERVAL XXX DAY per calcolare la scadenza, con somma soddisfazione ho visto che usando DATE_ADD("YYYY-MM_DD",INTERVAL XXX DAY) non genera le date obrobriose come il 31 febbraio
    ( \_/ )
    ( *.* )
    (> <) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.