Page 1 of 1

checkdate problem

Posted: Sun May 10, 2009 1:11 am
by traxy
Hey,

I am trying to use the checkdate() function but when I enter a date thats not valid it is always showing as valid.

eg. When I typed the date as, 04-77-2009 the code below will return:

------------------------------
day: 04 month: 77 year: 2004
bool(true)

Successfully added CD
Return to Administrator page
-------------------------------

Now obviously the month is incorrect, it always returns true no matter what day,date or year I put. It will only return false if I put something like 'hdakdjk' in the date field. Below is my code, please let me know if you see anything im doing wroing.

Code: Select all

 
<?php
 
$DBConnect=@mysqli_connect("localhost", "localhost", "password", "database")
    Or die("<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysqli_connect_errno($DBConnect) . ":" . mysqli_connect_error($DBConnect)) . "</p>";
 
$TableName="cd";
 
 
$date = explode("-", $_POST[DateArrived]);
echo "day: $date[0]"; // day
echo "month: $date[1]"; // month
echo "year: $date[2]"; //year
 
$day   = date( 'd', $date[0] );
$month = date( 'm', $date[1] );
$year  = date( 'Y', $date[2] );
 
var_dump(checkdate($month,$day,$year));
 
if (!$datecorrect = checkdate($month,$day,$year))
  {
     echo "INCORRECT DATE";
  } 
else{
$SQLstring = "INSERT INTO $TableName VALUES ('','$_POST[CdType]','$_POST[Artist]','$_POST[AlbumName]',STR_TO_DATE('$_POST[DateArrived]', '%d-%m-%Y'),'$_POST[CdPrice]')";
$QueryResult=@mysqli_query($DBConnect, $SQLstring)
    Or die("<p>Unable to add CD, please contact the web administrator</p>" . "<p>Error code " . mysqli_connect_errno($DBConnect) . ":" . mysqli_connect_error($DBConnect)) . "</p>" .
    '<a href="admin.html">Return to insert CD page</a>';
echo "<p>Successfully added CD</p>";    
}
 
echo '<a href="admin.html">Return to Administrator page</a>';
mysqli_close($DBConnect);  
 
 
?>
 
 
Thanks :)

Re: checkdate problem

Posted: Sun May 10, 2009 1:57 am
by mdk999
I think you are poisoning your data by using the date() function on the exploded parts. You should simply use the dates as is. The second parameter for date() is a timestamp not numeric date value.

Re: checkdate problem

Posted: Sun May 10, 2009 2:40 am
by mickd
Have a look in the PHP manual on what date, and also time, does exactly :)

Code: Select all

 
$day = $date[0];
$month = $date[1];
$year = $date[2];
 
The checkdate function has some nice examples too. If you got your code from the comments in checkdate, the reason why theirs work is because they used strtotime to turn the date into a timestamp (what the 2nd argument of date expects).

Re: checkdate problem

Posted: Sun May 10, 2009 4:47 am
by Defiline

Code: Select all

<?php
 
$day   = date('d');
$month = date('m');
$year  = date('Y');
 
// Another variant (We are not using %d since we need leader nuls)
echo ($date = sprintf('%s ... here is any separator %s / (too ., -) %s', date('d'), date('m'), date('Y')));
 
?>