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

M.chaudhry
  • 651
  • 1
  • 6
  • 13
  • 1
    This answer may help: http://stackoverflow.com/questions/12120433/php-mysql-insert-date-format – TonyWilk Mar 09 '14 at 13:43
  • And in case you missed it in the linked question, you're wonderfully open to SQL Injection. – Clockwork-Muse Mar 09 '14 at 14:04
  • @Clockwork-Muse missed it what sorry ? – M.chaudhry Mar 09 '14 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). – Clockwork-Muse Mar 09 '14 at 22:04

1 Answers1

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

Community
  • 1
  • 1
M.chaudhry
  • 651
  • 1
  • 6
  • 13