Solved

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

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

710 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