0

it is my html code

<div class="required" id="join_form">
                    <label for="DOB">DateOfBirth:</label>
                    <input type="date" name="date" id="date" required pattern="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" aria-required="true" aria-describedby="age-format" max="2004-12-31" min="1940-12-31" />
                    <span id="age-format" class="help" align="center">Format: mm/dd/yyyy, mm-dd-yyyy, mm.dd.yyyy, mm dd yyyy</span>
            </div>

javascript to validate this type=date in other browsers

var mydate = document.getElementById('date'),
    mydateError = document.getElementById('age-format');

mydate.addEventListener('input', function() {
    if (!mydate.value.match(/\d{4}-\d{1,2}-\d{1,2}/)) {
        mydateError.innerHTML = 'Please specify a valid date in the form 1940-2004 ';
        mydateError.style.display = 'inline-block;font-size:6pt;text-align:center;';
    } else {
        var value = new Date(date.value),
            min = new Date(date.min),
            max = new Date(date.max);
        if (value < min || value > max) {
            mydateError.innerHTML = 'Date has to be between ' + min.toDateString() + ' and ' + max.toDateString();
            mydateError.style.display = 'inline-block';
        } else {
            mydateError.style.display = 'none';
        }
    }
});

php validation and insert for date

$DOB=mysql_real_escape_string($_POST['date']);

$sql="INSERT INTO register(DOB) VALUES("$DOB");

i want to get whole date of birth like mm/dd/year but in my database i am only getting year like 1970 or 1987 like that....i cant figure out where i got wrong

4
  • 1
    This answer may help: stackoverflow.com/questions/12120433/… Commented Mar 9, 2014 at 13:43
  • And in case you missed it in the linked question, you're wonderfully open to SQL Injection. Commented Mar 9, 2014 at 14:04
  • @Clockwork-Muse missed it what sorry ? Commented Mar 9, 2014 at 17:24
  • 1
    You need to be using parameterized queries - towards the end of the first answer, there's a whole section regarding security of SQL (and PHP in particular). Essentially, trust NOTHING from the client (because not everybody can be). Commented Mar 9, 2014 at 22:04

1 Answer 1

0

PHP mysql insert date format

This Link did help me I used this to validate and insert type="date" in database

$timestamp = strtotime($DOB);
$date = date('d-m-y', $timestamp);
 $sql="INSERT INTO register(DOB) VALUES(FROM_UNIXTIME($timestamp))";

and my other Mistake was to set DOB as int in database

Data type required in a mysql for a date containing day-month-year

it should be DATE or DATETYPE

@Tonywilk Thankx :D

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.