What happens if we enter a value in the published field but it's not a valid date and time when I submit
the form.
The record is inserted successfully but the value inserted is all zeros.
That's because this field only accepts a date time string.
So if we try and insert something invalid the database server sets it to this empty date time value.
So before we insert it we need to validate this value just as we're validating that the title and the
content aren't empty.
To insert a date time value we need a string that consists of year month day hour minute and second
although as described here in the movie idb documentation we can use different delimiters and vary the
farm slightly.
Let's keep it simple however and specify that a date time string has to be in this format.
The date part separated by hyphens and the time separated by Colon's.
P. H.
P contains a date time class formulating dates and times to validate a string that contains a date.
We can create a date time object and this create from farm at function allows us to do just that.
We'll go into classes and objects later on in the course.
So for now we'll use the procedural style function to create a date time based on a farm that we pass
in the format as a string using these characters.
Each character representing the part of a date are time.
So if the value isn't empty let's create a date time object from it passing in the expected format.
This string represents the farm match we just saw in the database documentation.
The four digit year to date month and day separated by hyphens.
Then the space and hours minutes and seconds separated by comments.
If the function was an able to create a date time from that string it returns false.
In which case we add the validation error.
Let's give that a try.
If I enter an invalid date and time and submit the farm we get the error message.
If I enter a valid date and time then it validates correctly.
However watch what happens when I enter this state
and submit the form this date says the 30th of February.
But we don't get the validation error message.
In the database the last record has been inserted but the date and time have been set to all zeros again.
To see what happened let's print out the contents of the date time object using the date format function
and then exiting the script.
If I submit the farm with the same values as before with the date of the 30th of February we get this
date the 2nd of March.
What's happened is H HP has converted the invalid date to a valid one by carrying the extra days over
into the next month.
The database server won't do this though.
Hence the empty date time.
When we try and insert a rijkaard with this value.
When HP does this it generates a warning message that we can access by car they get last errors function.
So let's call this function and print out its return value.
When we submit the farm again we get this array with a warning that the date was invalid and also a
count of the number of warnings.
So we can check for any warning messages by checking that the number of warnings is more than zero.
And if so add a validation error message like that.
Now if we submit the same value we get a validation error message if we correct the date and submit
the farm.
The record is inserted in the database the correct date and time have been inserted successfully.