Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
writing a post to a file that is on same folder and same webserver 5 31
Button to go back 3 25
website maintenance mode 1 17
Number of hours between date in DB and now 8 18
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
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.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

772 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