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

Second time today I have a question which appears to give false errors to the user, thanks Treehouse.

Bummer! Make sure you return true for a valid date. Remember the function checkdate returns true if valid and false if not.

Well through all my testing, as you can use the code below. True appears to be returned when the date is valid, and false appears to come through when it's not. What am I doing wrong now Treehouse? What is the mystery error now?

index.php
<?php

echo (valid_sql_date('2011-10-10')) ? 'True' : 'False';

function valid_sql_date($date) {
    //add code here
    $dateMatch = explode('-', $date);
    if (count($dateMatch) != 3 ||
        strlen($dateMatch[0]) != 4 || 
        strlen($dateMatch[1]) != 2 || 
        strlen($dateMatch[2]) != 2 || 
        !checkdate($dateMatch[2], $dateMatch[1], $dateMatch[0])) {

      return false;   
    }

  return true;
}

Try this:

<?php

function valid_sql_date($date) {
    //add code here

    $dateMatch = explode('-', $date);

    // Use a regular expression match to check if the format is correct.
    if (! preg_match('/\d{4}-\d{2}-\d{2}/', $date)) return false;

    // If the format is correct return the result from checkdate
    return checkdate($dateMatch[2], $dateMatch[1], $dateMatch[0]);
}

-Ben

1 Answer

Nope, it gives me the same error: Bummer! Make sure you return true for a valid date. Remember the function checkdate returns true if valid and false if not.

Thanks for trying though!

Just noticed the indexes are wrong. Switch the 2 and the 1:

return checkdate($dateMatch[1], $dateMatch[2], $dateMatch[0]);

Sorry about that.

-Ben