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

Gurveer Singh
Gurveer Singh
11,353 Points

can't analyze problem, can anyone plz help

Before adding a date to a MySQL database, we want to make sure that it's in the proper format: YYYY-MM-DD

1 Split the date into its 3 parts using explode(), with the dash (-) character. 2 Check that each part is the proper number of digits. 3 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.

index.php
<?php

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

  {

    return false;

  }

  else
  {
     return true; 
   }
}