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

Validating Date stuck

I could use some help. I'm trying to prove it true...but the message I keep getting is that checkdate returns true if valid. So I don't know what's wrong. Does the date format given in the challenge dictate the order that I check the string lengths or do I have to do it in a particular order?

index.php
<?php

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

3 Answers

Matthew,

I think the issue may lay with the first part of your condition statement. In essence, you are stating that the number of items in $dateMatch cannot be identical to the number three. An SQL date is made up of three parts, YYYY-MM-DD. The index, $dateMatch[0], will correspond with the four-digit year. The next index, $dateMatch[1], will correspond with the two-digit month. The last index, $dateMatch[2], will correspond to the two-digit day.

I would suggest revising your condition statement to read as follows:

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

Thank you Eric! To make sure I'm on the right track- the count is counting the PARTS of the date and not the characters- correct?

The function, count, returns the number of elements in an array. In this case, your explode function would return three elements: one for the year, one for the month, and one for the day.

got it. thanks, again!