<p>Would it be worth doing some client-side validation first using javascript to check the date of birth field is entered correctly? You can use an <code>onChange</code> event on your field and then split the string on "/" and check each part is within a valid range <code>0-31</code>, <code>1-12</code>, <code>1900-2016</code>.</p>

<p>For serverside checking, you can use <a href="http://php.net/manual/en/function.strtotime.php" rel="nofollow">strtotime()</a> and check that the return isn't false.</p>

CC BY-SA 3.0
2016-07-21T08:28:08.160Z
A note, for UK dates, you would have to convert the format to US first before using strtotime.
James White
CC BY-SA 3.0
2016-07-21T08:42:24.750Z
Well spotted. You could also use [DateTime::createFromFormat](http://php.net/manual/en/datetime.createfromformat.php) if your form has separate fields for the day / month / year. If it's a single input field then you can't really tell if it's UK/US for certain dates.
Alexander Holsgrove
CC BY-SA 3.0
2016-07-21T08:46:21.193Z
Yeah, in my case the field is a single input, so the only thing to go by would be the `/` character to convert to `-` or use `explode()` on and break down into separate values like `$dd` `$mm` `$yyyy`
James White
CC BY-SA 3.0
2016-05-26T11:11:16.127Z
2016-05-26T11:11:16.127Z
Alexander Holsgrove
<p>Here's what I ended up doing. Despite having the format of DD/MM/YYYY on the front-end the Craft model actually stores the date in YYYY-MM-DD. Given the main the reason for wanting to perform server side validation is to check certain parts of the date i.e. year, you can create an array out of the string. You'll want to do a check to make sure the date string is a valid date, otherwise you'll get PHP errors with undefined offsets if trying to access array parts that aren't there.</p>

<p>Example:</p>

<ul>
<li><code>$value</code> - original date string submitted on the form, stored as YYYY-MM-DD by Craft</li>
<li><code>isValidDate</code> - Perform a check on the string value to ensure its a valid date value. <code>DateTime::createFromFormat</code> would return a date value if true, if it doesn't match the specified format, its false.</li>
<li><p><code>$dateOfBirth</code> - Using <code>explode()</code> create an array of the date string, accessible in parts as <code>[0]</code>, <code>[1]</code>, <code>[2]</code></p>

<pre><code>if($value != '')
{
    $isValidDate = DateTime::createFromFormat('Y-m-d', $value);

    if($isValidDate)
    {
        $dateOfBirth = explode('-', $value);
        $dobYear = $dateOfBirth[0];
        $dobMonth = $dateOfBirth[1];
        $dobDay = $dateOfBirth[2];

        // Further validation on date parts can occur here.
    }
    else 
    {
        // Not a valid date
    }   

}
</code></pre></li>
</ul>

CC BY-SA 4.0
2018-05-16T12:47:35.223Z
2018-05-16T12:47:35.223Z
James White
<p>I'm working a on website project which includes a web form which asks for various personal details, one of the fields on the form is Date of Birth.</p>

<p>The form is being validated by a custom plugin which is using <a href="https://craftcms.com/docs/plugins/models" rel="nofollow">models</a> for validation.</p>

<p>Currently the AttributeType for the Date Of Birth field is defined as <code>AttributeType::DateTime</code>, so bad values just get removed during POST, however it would be nice to try and catch values that fail a date format validation of <code>dd/MM/yyyy</code> so the user can correct it, otherwise the field value will simply be discarded and user will be unaware of this.</p>

<p>What is best way to check and catch that the Date Of Birth field value isn't in the required format? I want avoid any regex related parsing as I'm sure there are better ways to examine the format using other PHP date related functions.</p>

<plugin-development><datetime><validation><models><attribute>
Validating Date of Birth format from a form field value
# Validating Date of Birth format from a form field value

(Asked by: username_0 on 2016-05-25)

I'm working a on website project which includes a web form which asks for various personal details, one of the fields on the form is Date of Birth.

The form is being validated by a custom plugin which is using [models](https://craftcms.com/docs/plugins/models) for validation.

Currently the AttributeType for the Date Of Birth field is defined as `AttributeType::DateTime`, so bad values just get removed during POST, however it would be nice to try and catch values that fail a date format validation of `dd/MM/yyyy` so the user can correct it, otherwise the field value will simply be discarded and user will be unaware of this.

What is best way to check and catch that the Date Of Birth field value isn't in the required format? I want avoid any regex related parsing as I'm sure there are better ways to examine the format using other PHP date related functions.

**Tags:** plugin-development, datetime, validation, models, attribute

---

## 2 answers

### Answer by username_1 on 2016-05-26. Score: 1 | Accepted answer

Would it be worth doing some client-side validation first using javascript to check the date of birth field is entered correctly? You can use an `onChange` event on your field and then split the string on "/" and check each part is within a valid range `0-31`, `1-12`, `1900-2016`.

For serverside checking, you can use [strtotime()](http://php.net/manual/en/function.strtotime.php) and check that the return isn't false.

> **Comments:**
> **username_0**: A note, for UK dates, you would have to convert the format to US first before using strtotime.  
> **username_1**: Well spotted. You could also use [DateTime::createFromFormat](http://php.net/manual/en/datetime.createfromformat.php) if your form has separate fields for the day / month / year. If it's a single input field then you can't really tell if it's UK/US for certain dates.  
> **username_0**: Yeah, in my case the field is a single input, so the only thing to go by would be the `/` character to convert to `-` or use `explode()` on and break down into separate values like `$dd` `$mm` `$yyyy`  

---

### Answer by username_0 on 2018-05-16. Score: 1

Here's what I ended up doing. Despite having the format of DD/MM/YYYY on the front-end the Craft model actually stores the date in YYYY-MM-DD. Given the main the reason for wanting to perform server side validation is to check certain parts of the date i.e. year, you can create an array out of the string. You'll want to do a check to make sure the date string is a valid date, otherwise you'll get PHP errors with undefined offsets if trying to access array parts that aren't there.
Example:

$value - original date string submitted on the form, stored as YYYY-MM-DD by Craft
isValidDate - Perform a check on the string value to ensure its a valid date value. DateTime::createFromFormat would return a date value if true, if it doesn't match the specified format, its false.
$dateOfBirth - Using explode() create an array of the date string, accessible in parts as [0], [1], [2]
if($value != '')
{
    $isValidDate = DateTime::createFromFormat('Y-m-d', $value);

    if($isValidDate)
    {
        $dateOfBirth = explode('-', $value);
        $dobYear = $dateOfBirth[0];
        $dobMonth = $dateOfBirth[1];
        $dobDay = $dateOfBirth[2];

        // Further validation on date parts can occur here.
    }
    else 
    {
        // Not a valid date
    }   

}


---

