Solved

PHP Code & MySQL Database

Posted on 2014-07-22
4
1,089 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
  • 3
4 Comments
 
LVL 109

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 109

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 109

Accepted Solution

by:
Ray Paseur earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hbo knew my windows software 4 60
PHP Curl to output a url 7 46
Row insertion failed. Array 5 46
Button function on table is in trouble 3 20
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

809 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