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

simhub
simhub
26,544 Points

need help / CRUD Operations with PHP

can't pass question:

Challenge Task 1 of 1
Before adding a date to a MySQL database, we want to make sure that it's in the proper format: YYYY-MM-DD
Split the date into its 3 parts using explode(), with the dash (-) character.
Check that each part is the proper number of digits.
Check that the entered value is actually a valid date.
If any of the steps 1-3 fail, return false. If the date makes it through the validation, return true.


after sending my code i get this:
## Bummer! The function should return true for a valid date.

my code:

index.php
<?php

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

  return $result;
}

4 Answers

I tested your code and it seems to be working just fine. The problem seems to be that you are sending in the date in the wrong format. Your code checks for the format DD-MM-YYYY and not YYYY-MM-DD. Since MySQL saves Timestamps in the latter format you should just swap the order of the day with the year in your function. Like so:

Also, the checkdate() function needs the parameters to be in the order: month, day, year. Link

<?php

function valid_sql_date($date) {
    //add code here
  $result=false;
  $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])
    ) {
  $result=false;
    } else{  $result=true;}

  return $result;
}
Wayne Comber
Wayne Comber
8,169 Points

Try this. Only a small change i know, but the code interpreter can be a bit pedantic at times.

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

I did mine a while ago, but it validated with that sort of return structure.

simhub
simhub
26,544 Points

WHAAAT! a deceitful interpreter #!$

simhub
simhub
26,544 Points

Thank's for your reply Christian and i thing your right but i still getting this (#Bummer) - Bummer! The function should return true for a valid date!.

maybe its too early.
should try this task when i'm awake

Try this fiddle I made: http://phpfiddle.org/main/code/v9fa-b9b1

If you hit "Run" it will print whether the result is correct or not. Note the input string I send to the function.

simhub
simhub
26,544 Points

Holy.... didn't know there was a phpfiddle out there - cool!!

Yeah! It's not as good as jsfiddle, but it sure does suffice.

simhub
simhub
26,544 Points

sorry my bad!