Jump to content

how to validate a date based on user input from a form


webdeveloper123

Recommended Posts

Hi Guys,

My question is if I am doing something like this:

$customers['first_name'] = $_POST['fname'];
$customers['last_name'] = $_POST['lname'];


$errors['first_name']  = is_text($customers['first_name'], 2, 20)   ? '' : 'Must be 2-20 characters';
$errors['last_name']  = is_text($customers['last_name'], 2, 20)   ? '' : 'Must be 2-20 characters';

How would I validate a date? There seems to be a function called checkdate and validatedate is mentioned (although no longer on php.net) so It must have been taken out.

Btw, the date is not known before hand, as it's coming from a form which asks for users birthdate.

Many thanks

 

Link to comment
Share on other sites

You could use something like DateTime::createFromFormat() to try and parse the date.  If it fails to parse, invalid format.  If a valid date is created, you'll need to check it matches the input by comparing the input to the formatted date.  This is because the function will "fix" invalid dates, for example 07/35/2022 would parse as 08/04/2022.

<?php

$input = '07/31/2022';
$date = DateTime::createFromFormat('m/d/Y', $input);
if (!$date || $date->format('m/d/Y') !== $input){
    echo 'Invalid date';
} else {
    echo 'Valid date';
}

Any validate beyond that depends on what you need from the data.  For a birthday for example, you might validate it's in the past and not in the future. 

Link to comment
Share on other sites

10 minutes ago, webdeveloper123 said:

is there something like checkdate which will take 1 argument as a variable or as $customers['birthdate'] and see that the date is valid?

Adapt @kicken's code

function check_date($input, $format='m/d/Y')
{
    $date = DateTime::createFromFormat($format, $input);
    return ($date && $date->format($format) === $input);
}

if (!check_date($input) ) {
    $error['date'] = 'Invalid date';
}

 

Link to comment
Share on other sites

No. It is the expected input format that is being defined here.

Consider changing the function thus

function check_date($input, $format='m/d/Y')
{
    $date = DateTime::createFromFormat($format, $input);
    return ($date && $date->format($format) === $input) ? $date->format('Y-m-d') : false;
}

It then returns "false" if the date is invalid but returns the date in Y-m-d format if valid.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.