SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Entering a date in a table field

    Hello,

    I've created a field of type "date" in one of my tables. Usually I use CURDATE() to enter the date in the table like this:

    Code:
    $result = @mysql_query("INSERT INTO Table (name,date) VALUES ('$name',CURDATE())");
    But now I've changed the script so that I can enter any date I want. The date is parsed in the variables $year, $month and $day. My question is, how do I enter the date in the field date? I've tried this:

    Code:
    $result = @mysql_query("INSERT INTO Table (name,date) VALUES ('$name','$year-$month-$day')");
    but it doesn't work.

  2. #2
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That may be subtracting one value from the next. Either assign the three to another variable;

    Code:
    $date = $year . "-" . $month . "-" . $day;
    $result = @mysql_query("INSERT INTO Table (name,date) VALUES ('$name','$date')");
    Or may be this will work;

    Code:
    $result = @mysql_query("INSERT INTO Table (name,date) VALUES ('$name','". $year ."-" . $month . "-" . $day ."')");
    Hope that helps.

  3. #3
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does "it doesn't work" mean? what *IS* being inserted? or is there an error message? your code is fine. it's not subtracting the values since it's all in quotes (it would only subtract if it wasn't in quotes). you don't even need the dashes as long as the year is 4 digits and month/day are 2 digits. but, anyway, it should work fine. have you echoed the value of $year, $month, and $day to see that they contain the correct value?
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •