Solved

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

Posted on 2008-10-09
2
12,375 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MySQL database data submission 7 38
Problem to go to Web page 2 50
Best Practice CSS floating images 2 15
PHP string issue 5 17
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now