?
Solved

PHP Code & MySQL Database

Posted on 2014-07-22
4
Medium Priority
?
1,150 Views
Last Modified: 2014-07-22
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

0
Comment
Question by:wantabe2
[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
  • 3
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40211548
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40211561
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
 
LVL 15

Author Comment

by:wantabe2
ID: 40211657
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40211728
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

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

770 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