Link to home
Start Free TrialLog in
Avatar of jaxstorm
jaxstormFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Converting PHP Date dd/mm/yyyy to SQL date YYYY-MM-DD

I'm having an annoying problem.

Got a date textbox where the entry is dd/mm/yyyy and I've converted the date for SQL into YYYY-MM-DD.

Then I run the checkdate command to confirm it's a valid date ;


it checks the date fine, but then, when putting it into the DB it puts it in mm-dd-yyyy format.

So, if I enter 31/7/2008 it'll convert the 31 into months and end p with some date thats in 2010!


$Header = $_POST['Header'];
$Yeargroup = $_POST['Yeargroup'];
$Details = $_POST['Details'];
$Footer = $_POST['Footer'];
$Deadline = $_POST['Deadline'];
$Date = $_POST['Date'];
 
 
list($DD,$MM,$YY) = explode("/",$Date);
if(!checkdate($MM,$DD,$YY))
	{
    echo 'The date is invalid, please enter a date in the format 
DD/MM/YY';
 
}else{
 
$Date = date('Y-m-d', strtotime($Date));
 
$Query="INSERT INTO bulletin (Header, YearGroup, Details, Deadline, Footer, Date)VALUES 
('$Header','$Yeargroup','$Details','$Deadline','$Footer','$Date')";
 
mysql_query($Query) or die ('Error updating database');
 
printf ("Bulletin Item Added<br><br><br>Refreshing...");
 
}
?>

Open in new window

Avatar of jaxstorm
jaxstorm
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I've now discovered that the problem is with the following line of code, and that the checkdate function is fine, question changed
$Date = date('Y-m-d', strtotime($Date));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jaxstorm
jaxstorm
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial