PHP Code & MySQL Database

I have a MySQL database with a table named datepicker. Currently, on the page I have a nice pop up calendar that the user picks a date & the date is displayed on the page as 07/02/2014. When I submit the page, it gets dumped into the table in the same format 07/02/2014. How can I easily edit the code below to make the date either display YYYY-MM-DD on the page or get entered into the DB table in the YYY-MM-DD format?

<html lang="en">
<head>
  <meta charset="utf-8" />

  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
    $( "#format" ).change(function() {
      $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
    });
  });
  </script>
</head>
<body>
 
<p><b>Due Date:</b> <br> <input type="date" name="datepicker" id="datepicker" size="15" /></p>

</body>
</html>

Open in new window

LVL 15
wantabe2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
In the PHP script, use strtotime() to convert the date to the Unix timestamp.  Then use date('c') to convert the Unix timestamp into the ISO-8601 formatting for the DATETIME string.
0
Ray PaseurCommented:
Hmm... But you may not need to do that! Have a look at this script.
http://iconoun.com/demo/temp_wantabe2.php?datepicker=2014-07-04

<?php // demo/temp_wantabe2.php
error_reporting(E_ALL);

var_dump($_GET);

?>
<html lang="en">
<head>
  <meta charset="utf-8" />

  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
    $( "#format" ).change(function() {
      $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
    });
  });
  </script>
</head>
<body>

<form>

<p><b>Due Date:</b> <br> <input type="date" name="datepicker" id="datepicker" size="15" /></p>

<input type="submit" />
</form>

</body>
</html>

Open in new window

0
wantabe2Author Commented:
That didn't work but I was able to get this code to partially work. The only issue with the code below is, I have to choose ISO8061 from a drop down box before it will put it in the correct YYYY-MM-DD format. Is there any way to make it automatically change it to YYYY-MM-DD?

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Format date</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();
$( "#format" ).change(function() {
$( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" size="30"></p>
<p>Format options:<br>
<select id="format">
<option value="mm/dd/yy">Default - mm/dd/yy</option>
<option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
<option value="d M, y">Short - d M, y</option>
<option value="d MM, y">Medium - d MM, y</option>
<option value="DD, d MM, yy">Full - DD, d MM, yy</option>
<option value="&apos;day&apos; d &apos;of&apos; MM &apos;in the year&apos; yy">With text - 'day' d 'of' MM 'in the year' yy</option>
</select>
</p>
 
<p><b>Due Date:</b> <br> <input type="date" name="datepicker" id="datepicker" size="15" /></p>

</body>
</html>

Open in new window

0
Ray PaseurCommented:
That didn't work...
It worked for me, giving the ISO-8601 format when I tested it in Chrome.  However I tested it again in Firefox and it produced the wrong date format.  You might be a little suspect of the jQuery script here, since it didn't work the same cross-browser.

Here is an amendment that might give you a good solution.  Bear in mind that any external data must be considered to be tainted and is therefore an attack vector.  

<?php // demo/temp_wantabe2.php
error_reporting(E_ALL);

$date 
= !empty($_GET['datepicker']) 
? date('c', strtotime($_GET['datepicker'])) 
: FALSE
;
if ($date)
{
    echo PHP_EOL . "DATE: $date";
}


?>
<html lang="en">
<head>
  <meta charset="utf-8" />

  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
    $( "#format" ).change(function() {
      $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
    });
  });
  </script>
</head>
<body>

<form>

<p><b>Due Date:</b> <br> <input type="date" name="datepicker" id="datepicker" size="15" /></p>

<input type="submit" />
</form>

</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.