Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
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

Avatar of wantabe2

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial