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 trialsimhub
26,544 Pointsneed 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:
<?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
Christian Andersson
8,712 PointsI 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
8,169 PointsTry 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
26,544 PointsWHAAAT! a deceitful interpreter #!$
simhub
26,544 PointsThank'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
Christian Andersson
8,712 PointsTry 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
26,544 PointsHoly.... didn't know there was a phpfiddle out there - cool!!
Christian Andersson
8,712 PointsYeah! It's not as good as jsfiddle, but it sure does suffice.
simhub
26,544 Pointssorry my bad!