Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
How do we assure that the date is in the correct format and is actually a valid date? In this video, we'll explore one option for validating string formats and checking for a valid date.
Documentation
explode() Split a string by string
strlen() Get string length
checkdate() Validate a Gregorian date
Regular Expressions
Another option you often see for validating strings, is the use of regular expressions. A regular expression (regex or regexp for short) is a special text string for describing a search pattern. Regular expressions are a great tool to learn. Most applications as well as most programming languages include some form of regular expression matching.
PHP includes a number of Regular Expression Functions. We could use preg_match to match our dates as well.
Example:
if (preg_match('/^(\d{2})\/(\d{2})\/(\d{4})$/', $date, $dateMatch)) {
if (!checkdate($dateMatch[1], $dateMatch[2], $dateMatch[3])) {
$error_message = 'Invalid Date';
}
} else {
$error_message = 'Invalid Date format';
}
This matches a string that starts with (^) 2 digits (\d{2}), followed by a forward slash (\/), then another 2 digits (\d{2}) followed by another forward slash (\/), and finally 4 digits (\d{4}) at the end($).
-
0:00
The task form is working and adding values to the database but
-
0:04
we don't have any data integrity when it comes to dates.
-
0:07
The project is a drop down and the filtering for integer ensures
-
0:11
that the time is a number but currently we except anything in the date field.
-
0:16
We have suggested the format we want, a two-digit month
-
0:21
followed by a forward slash and a two digit day then another forward slash and
-
0:26
a four digit year but there's nothing enforcing that format.
-
0:30
So it's all too easy to add an invalid format whether we're trying to or
-
0:34
not or even an invalid date.
-
0:37
Not all months have 31 days.
-
0:39
So, how do we sure that the date is in the correct format and
-
0:43
is actually a valid date?
-
0:45
Let's jump back into work spaces and explore one option.
-
0:49
In task.php, after we've collected the user data, let's check the date variable.
-
0:56
First, we need to get each piece of the date so we'll use the explode function.
-
1:01
Explode takes two parameters, a string delimiter and the string and
-
1:06
returns an array.
-
1:07
So let's assign this to a variable dateMatch.
-
1:13
We use explode, And first the string delimiter.
-
1:18
The string delimiter determines where the string is split.
-
1:22
In the case of the date, we're going to use the forward slash
-
1:25
since each piece of the date should be separated with a forward slash.
-
1:30
Next we use the date for the string.
-
1:35
Now let's var dump the dateMatch.
-
1:43
This will show us what we have so far.
-
1:45
Now, let's go back to the browser.
-
1:48
Let's start with the completely invalid date and just type today.
-
1:53
When we submit the form, we see that the date match is set to a single item array
-
1:58
with the entire string as the first element.
-
2:02
Now, let's change this to put in a valid date that's just in the wrong format.
-
2:11
This is a valid date but not the format we want.
-
2:15
Now we see that dateMatch is a three item array
-
2:18
with each piece of the date as a separate element.
-
2:22
Excellent, just what we want.
-
2:24
Now, let's start writing the conditional.
-
2:27
Let's add an elseif to this conditional.
-
2:38
Elseif count dateMatch does not = 3.
-
2:43
Then we're going to set our error_message.
-
2:50
Invalid Date.
-
2:53
We also want to give this error if the date is not the correct format.
-
2:58
So, let's check that each piece of the array is the correct link.
-
3:00
Or, string length.
-
3:09
DateMatch 0.
-
3:11
Meaning, our first element is not = to 2 or
-
3:16
the string length, DateMatch 1.
-
3:22
Meaning, our second element is not = to 2 and
-
3:28
also string length, dateMatch 2.
-
3:34
Our third element is not = to 4.
-
3:38
And finally, we want to check that the date is actually valid and
-
3:42
that the user has not entered the day that does not exist.
-
3:46
For example, day 31 in a month that only has 30 days.
-
3:50
We can use the checkdate function for this or not checkdate.
-
3:58
checkdate takes three parameters.
-
4:01
First the month which is our dateMatch 0.
-
4:05
And then the day, dateMatch 1 and
-
4:10
then the year, dateMatch 2.
-
4:17
Great job. Let's take out the var dump and
-
4:18
run some tests in the browser.
-
4:25
When we refresh the page, we see our error for
-
4:28
the required fields because we check for that first.
-
4:32
So let's make sure all the fields are filled in.
-
4:40
Now we see invalid date because the year only has two characters and not four.
-
4:46
Let's change this to 9/31.
-
4:49
2016.
-
4:52
We still see invalid date because
-
4:57
September does not have 31 days.
-
5:03
Now let's change this to 8/31.
-
5:06
This time our date was added because the date is valid, great job.
You need to sign up for Treehouse in order to download course files.
Sign up