question on the function 'date' in php

Discussion in 'PHP' started by alanX, Jun 11, 2006.

  1. #1
    hi:
    when inserting the date into the mysql:

    mysql_select_db('date');
    $query = "insert into date values
    ('".DATE('j F Y H:i')."')";
    $result = mysql_query($query);

    the date stored in mysql are always like "0000-00-00" rather than the normal
    format(e.g '12 June 2006 03:10')

    why?
     
    alanX, Jun 11, 2006 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,826
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #2
    If you have set the column to "date" it will store it in that format.

    Remember Y2K, it was because people weren't doing that.

    You'll see some systems drop the - between the values, the important thing is that it's in that format. Better for sorting, retrieving, everything.

    And your "June" is someone else's "juin" or 六月, liù yuè

    Once you have the date back from the database you can convert it to your "nice" format very easily.
     
    sarahk, Jun 11, 2006 IP
  3. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for your reply:

    i have set a column to "date" already; the problem still occurs.
     
    alanX, Jun 11, 2006 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,826
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Indeed, it's BECAUSE you have the column set to "date"

    It's bad to have it any other way.

    change your query to
    $query = "insert into date values
    ('".DATE('Y-m-d')."')";
    $result = mysql_query($query);
    Code (markup):
    and it will save nicely.

    When you retrieve the date you just need to rework it to make it "nice"

    The only other way is to change the column to a varchar but that is NOT recommended.
     
    sarahk, Jun 11, 2006 IP
  5. sacx13

    sacx13 Active Member

    Messages:
    438
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #5
    if the date field si date type then try to use now() mysql function.
    
    insert into date values (now());
    
    Code (markup):
    Regards
     
    sacx13, Jun 12, 2006 IP