?
Solved

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

Posted on 2008-10-09
2
Medium Priority
?
12,476 Views
Last Modified: 2013-12-20
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

0
Comment
Question by:jaxstorm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 8

Author Comment

by:jaxstorm
ID: 22676530
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

0
 
LVL 8

Accepted Solution

by:
jaxstorm earned 0 total points
ID: 22676578
Solved it myself
$Date = split('/', $Date);
 
$NewDate = $Date[2].'-'.$Date[1].'-'.$Date[0];

Open in new window

0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question