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
Mayur Pande
Courses Plus Student 11,711 Points

Sanitizing 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
Simon Coates
28,692 Points

You 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
Mayur Pande
Courses Plus Student 11,711 Points

Thanks 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;

    $tutoremail = trim(filter_input(INPUT_POST,"tutoremail",FILTER_SANITIZE_EMAIL));
    $starttime = trim(filter_input(INPUT_POST,"starttime",FILTER_SANITIZE_STRING));
    $location = trim(filter_input(INPUT_POST,"location",FILTER_SANITIZE_STRING));
    $class = trim(filter_input(INPUT_POST,"class",FILTER_SANITIZE_STRING));
    $driveremail = trim(filter_input(INPUT_POST,"driveremail",FILTER_SANITIZE_EMAIL);

within this;

$app->post('/addDriver', function(Request $request) use($app) {
    $tutoremail = $app['request']->get('tutoremail');
    $starttime = $app['request']->get('starttime');
    $location = $app['request']->get('location');
    $class = $app['request']->get('class');
    $driveremail = $app['request']->get('driveremail');
    $postcode = $app['request']->get('postcode'); //honey pot var should be empty
    $post = array($tutoremail,$starttime,$location,$class,$driveremail,$postcode); 

   //code to redirect to new page

Simon Coates
Simon Coates
28,692 Points

there 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.