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

PHP Validate Date Code Challenge: Problem

Does anybody know what is wrong with my code? It works fine locally...

index.php
<?php

function valid_sql_date($date) {
    //add code here
  $datearray = explode('-', $date);

  if (count($datearray) != 3
            || strlen($datearray[0]) != 4
            || strlen($datearray[1]) != 2
            || strlen($datearray[2]) != 2
            || !checkdate($datearray[2], $datearray[1], $datearray[0])) {
    return false;
  } else {
    return true;
  }

}

2 Answers

I'm not sure why it's failing because you are correct, I'm guessing its to do with spacing or something in the Treehouse editor.

The code I wrote is the exact same as yours and it works

<?php

function valid_sql_date($date) {
  //1. Split the date into its 3 parts using explode(), with the dash (-) character.
  $datearray = explode('-', $date);

  //2. Check that each part is the proper number of digits.
  //3. Check that the entered value is actually a valid date.
  if ( count($datearray) != 3 
      || strlen($datearray[0]) != 4 
      || strlen($datearray[1]) != 2 
      || strlen($datearray[2]) != 2 
      || !checkdate($datearray[1], $datearray[2], $datearray[0]) ) {
    return false;
  } else { 
    return true;
  }
}

The code above is not the same.

lukej has:

!checkdate($datearray[2], $datearray[1], $datearray[0])

and Liam has:

!checkdate($datearray[1], $datearray[2], $datearray[0])