Add Current Year To User-Inputted Date

Discussion in 'PHP' started by pumog99, Dec 23, 2012.

  1. #1
    I have a date field in a form.

    If a user enters 12/23 instead of 12/23/12 I want to add the current year before putting it into the datatbase. The format in the database is Y-m-d because it's a date field.

    How do I modify this code to do that? I'm sorry I'm a bit weak with dates, especially if I have to stray from the standard date code!


    $this_date = date("Y-m-d", strtotime($_POST['this_date ']));
    $query
    ="insert into table (this_date) values ('$this_date')";
    If a user enters 12/23/12 or 12/23/2012, I do not need to add the current year, and the above code will work, so I need the code to take into account a user entering the year OR not entering it.
     
    pumog99, Dec 23, 2012 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    It will be easier if you set up your form to use <select>s for each component of your date, rather than asking the user to enter it into a text box. But your script will still have to validate the individual components and assemble them into the format required by your database.

    You should never insert form data (like $_POST['this_date']) into a database because it leaves your script vulnerable to hacking. You should always validate all form data completely using functions like mysql_real_escape_string() to make sure it doesn't contain any malicious code. Search on "php mysql security" and you'll find some good advice.
     
    rainborick, Dec 23, 2012 IP