Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP CRUD Operations with PHP Creating Records Validating User Data

Stuck on checking for matching dates

Sorry but I'm stuck on checking on validating dates and I'm hoping one of you will be able to help me.

https://teamtreehouse.com/library/crud-operations-with-php/creating-records/validating-user-data

index.php
<?php

function valid_sql_date($date) {
    //add code here
  $dateMatches = explode('/',$date);
  if(count($dateMatches) != 3 ||
     strlen($dateMatches[0]) != 4 ||
     strlen($dateMatches[1]) != 2 ||
     strlen($dateMatches[2]) != 2
  ) {
     return false;
  }else return true;
}

1 Answer

Daniel Marin
Daniel Marin
8,021 Points

Hi you can add some additional checking to check if the date is numeric because the checkdate function will throw a notice.
Basically this is how you can do it:

<?php
function valid_sql_date($date) {
    //add code here
    $parts = explode('-', $date);
    $year = $parts[0];
    $month = $parts[1];
    $day = $parts[2];

    if(checkdate($month, $day, $year))
        return true;

    return false;
}