checkdate problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
traxy
Forum Newbie
Posts: 9
Joined: Thu Mar 26, 2009 11:34 pm

checkdate problem

Post 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 :)
Last edited by Benjamin on Sun May 10, 2009 12:37 pm, edited 1 time in total.
Reason: Changed code type from text to php.
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: checkdate problem

Post 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.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Re: checkdate problem

Post 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).
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: checkdate problem

Post 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')));
 
?>
Post Reply