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

Miguel Sánchez
Miguel Sánchez
10,365 Points

Anyone can help me with thiis CRUD exercise, please? :( Thanks :)

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

//This is my code

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; } else { return true;
} }

index.php
<?php

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; 
   } else {
      return true;  
   }
}
Mark Miller
Mark Miller
45,831 Points

You need to negate your checkdate function with an apostrophe so it will return false if it is not good. And I think we've got the order in YYYY-MM-DD so position [0] is year, [1] is month and [2] is day, and the checkdate function is programmed (http://php.net/manual/en/function.checkdate.php) to do month, day, year. So, it should go 1, 2, 0 in a negated !checkdate().

1 Answer

Angela Visnesky
Angela Visnesky
20,927 Points

Hi Miguel! You are really close to the right answer. I had to look at a couple different answers to this before I spotted the error. You need to declare a variable that is equal to true or false, depending on the date, and return that variable.

<?php
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[1], $dateMatch[2], $dateMatch[0]))
  {
      $answer = false;
    } else {  
       $answer = true;
    }
    return $answer;
}

I hope this helps!