SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member dgij's Avatar
    Join Date
    Apr 2003
    Location
    Spain
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert String to date

    I have a date field in my MySQL database, and I also have a form to get the birthday from the user. In the form I have 3 SELECT fields, one for the day, one for the month and one for the year of birth. How can I save this date, in format YYYY-MM-DD, in the database, knowing that the date to save comes from a string?
    Maybe the real question is: how can I save a string like "2003-06-20" as a MySQL date piece of data?

    Thanks in advance.

  2. #2
    SitePoint Addict Ramiro S's Avatar
    Join Date
    May 2003
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a Date type column in your mysql database... so it easy:
    PHP Code:
    $Date2Store=$_POST['year'] . "-" $_POST['month'] . "-" $_POST['day']; 
    that var holds the YYYY-MM-DD

    if the fields in the form doesn't use the same format (like YY instead of YYYY) try this:
    PHP Code:
    $Date2Store=date("Y-m-d"mktime(0,0,0,$_POST['month'], $_POST['day'], $_POST['year'])); 
    That will do it.
    Quasar - Web Development - Free Avatars

  3. #3
    SitePoint Enthusiast CamelToe's Avatar
    Join Date
    May 2003
    Location
    Canada
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if I'm following correctly but how about

    PHP Code:
    $y $_POST['Year'];
    $m $_POST['Month'];
    $d $_POST['Day'];

    $date '$y' '/' '$m' '/' .  '$d';
    ...
    $sql INSERT INTO YourTable (DobVALUES ('$date'); 
    I am assuming your Form Names are Year, Month, Day in the above code. And your Mysql Date field is called Dob.

  4. #4
    SitePoint Member dgij's Avatar
    Join Date
    Apr 2003
    Location
    Spain
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your quick answers, guys
    Ramiro, both solutions worked, Im taking the first one because of its simplicity.
    CamelToe, just one little detail: you must double-quote your varibles like this to get it working:
    PHP Code:
    $y $_POST['Year'];
    $m $_POST['Month'];
    $d $_POST['Day'];

    [
    B]$date "$y"/" "$m"/" .  "$d";[/B]
    ...
    $sql INSERT INTO YourTable (DobVALUES ('$date'); 
    (at least in my server worked this way).

    Thank you both again!

  5. #5
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although MySQL doesn't care, strictly speaking you should be using '-' instead of '/' to insert dates to db

    http://www.mysql.com/doc/en/DATETIME.html

    As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A ``relaxed'' syntax is allowed here, too. For example, '98-12-31', '98.12.31', '98/12/31', and '98@12@31' are equivalent.
    Work smarter, not harder. -Scrooge McDuck


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
  •