Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

910 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

24 Experts available now in Live!

Get 1:1 Help Now