Solved

PHP Code & MySQL Database

Posted on 2014-07-22
4
1,104 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 110

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 110

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Html date format 9 20
restriction of entering a a page 5 34
Dynamic Table mySQL stored procedure 5 37
title attribute 5 27
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
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 …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

726 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