Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Mayur Pande
Courses Plus Student 11,711 PointsSanitizing a datetime variable
Is it possible to sanitize a datetime variable in php. Similar to what Alena Holligan done in her suggest.php with emails and texts for post variables?
1 Answer

Simon Coates
28,692 PointsYou could probably process it as a string (including sanitising as a string) and then see if it represents a valid datetime format before using it. If you push an invalid string into a datetime, in this example at least, it throws an exception.
try {
$date = new DateTime('2ss000-01-01');
} catch (Exception $e) {
}
Or you could validate using regex. I'm sure there's probably some official advise somewhere on what you're supposed to do. I've seen a couple example of using regex to validate format or replace characters that aren't digits or a slash, but not whether the date is an actual date. There is a method called checkdate.
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsThanks will give it a try.
Don't suppose you know about sanitizing data with silex/symfony?
I am not sure if I can use the method Alena used for sanitizing data
i.e. Can I use this;
within this;
Simon Coates
28,692 PointsSimon Coates
28,692 Pointsthere is a filter_var method that works mostly the same. Don't know symfony, so I don't know if symfony provides you with anything that helps with filtering.