Jump to content

checkdate problem


traxy

Recommended Posts

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.

 

<?php

$DBConnect=@mysqli_connect("localhost", "user", "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 :)

Link to comment
https://forums.phpfreaks.com/topic/157560-checkdate-problem/
Share on other sites

When you give date() an invalid value it returns 01-01-1970.

 

If $_POST[DateArrived] contains a date in some particular format, why on earth are you feeding the parts of it to the date() function, which as already stated accepts a Unix Timestamp as the second parameter.

Link to comment
https://forums.phpfreaks.com/topic/157560-checkdate-problem/#findComment-830837
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.