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 Chechdate Challenge Problems

Hi guys,

I'm struggling with this challenge at the moment.

We've been asked to check that a date entered is in the YYYY-MM-DD format.

There's three steps to the challenge:

  • Split the date using the PHP explode function
  • Check that each of the objects in the exploded array is the correct length
  • Use the PHP checkdate function to make sure it is a valid date.

Once that's all run the function should return true or false.

It's really got me stumped.

Any help would be greatly appreciated.

Cheers Don :-)

index.php
<?php

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

  $date = '2017-03-21';

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

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

PS I've tried running my code both with and without setting the $date variable, and also setting it as a string as it currently is, or just as 2017-03-21

Cheers Don

1 Answer

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hi Don Macarthur,

I have watched previous Alena video before this challenge and I have noticed that you are using checkdate function different than it's supposed to be used. For example checkdate function takes 3 arguments in specific order.

<?php
// checkdate( Month, Date, Year )
!checkdate($dateMatch[1], $dateMatch[2], $dateMatch[0])
?>

and I noticed if you want to pass this challenge you have to comment your $date variable.

//$date = '2017-03-21';

I hope it helps:)

Thanks Algirdas,

I must have missed the part in Alena's video where it stated the checkdate function needed the values in a specific order. D'oh! :-)

On the $date variable, I only set that as I was unsure if the challenge was falling over due to not having an actual date to check. I tried it both with and without setting it and had no joy either way.

Thanks again for the help :-)