1 00:00:00,550 --> 00:00:04,200 The task form is working and adding values to the database but 2 00:00:04,200 --> 00:00:07,910 we don't have any data integrity when it comes to dates. 3 00:00:07,910 --> 00:00:11,880 The project is a drop down and the filtering for integer ensures 4 00:00:11,880 --> 00:00:16,960 that the time is a number but currently we except anything in the date field. 5 00:00:16,960 --> 00:00:21,590 We have suggested the format we want, a two-digit month 6 00:00:21,590 --> 00:00:26,250 followed by a forward slash and a two digit day then another forward slash and 7 00:00:26,250 --> 00:00:30,200 a four digit year but there's nothing enforcing that format. 8 00:00:30,200 --> 00:00:34,120 So it's all too easy to add an invalid format whether we're trying to or 9 00:00:34,120 --> 00:00:37,160 not or even an invalid date. 10 00:00:37,160 --> 00:00:39,470 Not all months have 31 days. 11 00:00:39,470 --> 00:00:43,660 So, how do we sure that the date is in the correct format and 12 00:00:43,660 --> 00:00:45,780 is actually a valid date? 13 00:00:45,780 --> 00:00:49,945 Let's jump back into work spaces and explore one option. 14 00:00:49,945 --> 00:00:55,620 In task.php, after we've collected the user data, let's check the date variable. 15 00:00:56,990 --> 00:01:01,670 First, we need to get each piece of the date so we'll use the explode function. 16 00:01:01,670 --> 00:01:06,040 Explode takes two parameters, a string delimiter and the string and 17 00:01:06,040 --> 00:01:07,470 returns an array. 18 00:01:07,470 --> 00:01:10,095 So let's assign this to a variable dateMatch. 19 00:01:13,220 --> 00:01:18,770 We use explode, And first the string delimiter. 20 00:01:18,770 --> 00:01:22,300 The string delimiter determines where the string is split. 21 00:01:22,300 --> 00:01:25,990 In the case of the date, we're going to use the forward slash 22 00:01:25,990 --> 00:01:29,580 since each piece of the date should be separated with a forward slash. 23 00:01:30,630 --> 00:01:35,410 Next we use the date for the string. 24 00:01:35,410 --> 00:01:39,691 Now let's var dump the dateMatch. 25 00:01:43,031 --> 00:01:44,790 This will show us what we have so far. 26 00:01:45,850 --> 00:01:47,020 Now, let's go back to the browser. 27 00:01:48,100 --> 00:01:51,200 Let's start with the completely invalid date and just type today. 28 00:01:53,830 --> 00:01:58,556 When we submit the form, we see that the date match is set to a single item array 29 00:01:58,556 --> 00:02:02,160 with the entire string as the first element. 30 00:02:02,160 --> 00:02:06,140 Now, let's change this to put in a valid date that's just in the wrong format. 31 00:02:11,910 --> 00:02:15,900 This is a valid date but not the format we want. 32 00:02:15,900 --> 00:02:18,810 Now we see that dateMatch is a three item array 33 00:02:18,810 --> 00:02:21,390 with each piece of the date as a separate element. 34 00:02:22,480 --> 00:02:24,450 Excellent, just what we want. 35 00:02:24,450 --> 00:02:26,140 Now, let's start writing the conditional. 36 00:02:27,460 --> 00:02:29,620 Let's add an elseif to this conditional. 37 00:02:38,038 --> 00:02:43,826 Elseif count dateMatch does not = 3. 38 00:02:43,826 --> 00:02:47,070 Then we're going to set our error_message. 39 00:02:50,735 --> 00:02:53,930 Invalid Date. 40 00:02:53,930 --> 00:02:58,160 We also want to give this error if the date is not the correct format. 41 00:02:58,160 --> 00:03:00,888 So, let's check that each piece of the array is the correct link. 42 00:03:00,888 --> 00:03:05,948 Or, string length. 43 00:03:09,475 --> 00:03:10,340 DateMatch 0. 44 00:03:11,660 --> 00:03:16,729 Meaning, our first element is not = to 2 or 45 00:03:16,729 --> 00:03:22,341 the string length, DateMatch 1. 46 00:03:22,341 --> 00:03:28,272 Meaning, our second element is not = to 2 and 47 00:03:28,272 --> 00:03:34,591 also string length, dateMatch 2. 48 00:03:34,591 --> 00:03:37,560 Our third element is not = to 4. 49 00:03:38,670 --> 00:03:42,950 And finally, we want to check that the date is actually valid and 50 00:03:42,950 --> 00:03:46,110 that the user has not entered the day that does not exist. 51 00:03:46,110 --> 00:03:50,470 For example, day 31 in a month that only has 30 days. 52 00:03:50,470 --> 00:03:57,340 We can use the checkdate function for this or not checkdate. 53 00:03:58,810 --> 00:04:01,450 checkdate takes three parameters. 54 00:04:01,450 --> 00:04:05,143 First the month which is our dateMatch 0. 55 00:04:05,143 --> 00:04:10,413 And then the day, dateMatch 1 and 56 00:04:10,413 --> 00:04:17,170 then the year, dateMatch 2. 57 00:04:17,170 --> 00:04:18,897 Great job. Let's take out the var dump and 58 00:04:18,897 --> 00:04:20,148 run some tests in the browser. 59 00:04:25,807 --> 00:04:28,485 When we refresh the page, we see our error for 60 00:04:28,485 --> 00:04:32,160 the required fields because we check for that first. 61 00:04:32,160 --> 00:04:34,160 So let's make sure all the fields are filled in. 62 00:04:40,780 --> 00:04:46,830 Now we see invalid date because the year only has two characters and not four. 63 00:04:46,830 --> 00:04:49,998 Let's change this to 9/31. 64 00:04:49,998 --> 00:04:51,211 2016. 65 00:04:52,565 --> 00:04:57,347 We still see invalid date because 66 00:04:57,347 --> 00:05:03,160 September does not have 31 days. 67 00:05:03,160 --> 00:05:06,697 Now let's change this to 8/31. 68 00:05:06,697 --> 00:05:12,130 This time our date was added because the date is valid, great job.