Date Format on an HTML Form - Passing to PHP

Following on from this - http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/HTML/Q_28235999.html

I have the script part as :

  <script>
 $(function() {
    $( ".DateFrom" ).datepicker({ changeMonth: true,changeYear: true,yearRange: "1877:2013",dateFormat: "dd-mm-yy" });
    $( ".DateTo" ).datepicker({ changeMonth: true,changeYear: true,yearRange: "1877:2013",dateFormat: "dd-mm-yy" });
  });
  </script>

Open in new window


I'm passing these dates through to a php page. How can I convert these dates to yyyy-mm-dd to run a query against the database with them?
PeterErhardAsked:
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:
Have you tried changing the dateFormat in the jQuery Datepicker call?

<?php // RAY_temp_petererhard.php
error_reporting(E_ALL);
echo '<pre>';

var_dump($_POST);

?>
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script>
$(function() {
    $( ".DateFrom" ).datepicker({ changeMonth: true,changeYear: true,yearRange: "1877:2013",dateFormat: "yy-mm-dd" });
    $( ".DateTo" ).datepicker({ changeMonth: true,changeYear: true,yearRange: "1877:2013",dateFormat: "yy-mm-dd" });
});
</script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form method="post">
  <input type="text" class="DateFrom" name="f" />
  <input type="text" class="DateTo"   name="t" />
<input type="submit" />
</body>
</html>

Open in new window

0
PeterErhardAuthor Commented:
Yeah, but I don't want to confuse people with a front-end display of  yyyy-mm-dd
0
Ray PaseurCommented:
Code snippet added here:
http://www.experts-exchange.com/Database/MySQL/Q_28237522.html#a39486483

I didn't find the front end display confusing at all.  Try it and see if it makes sense to you.

Also, please read this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

In PHP we reformat dates with a combination of strtotime() and date().  There are OO notations for the DateTime class, too.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Ray PaseurCommented:
Here's another version.  Enjoy... ~Ray
http://www.laprbass.com/RAY_temp_petererhard.php

<?php // RAY_temp_petererhard.php
error_reporting(E_ALL);
echo '<pre>';


// IF THE REQUEST HAS POST DATA
if (!empty($_POST))
{
    // SHOW THE RECEIVED DATES
    var_dump($_POST);

    // REFORMAT THE DATES
    $dates = array( 'f', 't' );
    foreach ($dates as $d)
    {
        $_POST[$d] = date('Y-m-d', strtotime($_POST[$d]));
    }

    // SHOW THE REFORMATTED DATES
    var_dump($_POST);
}
echo '</pre>';

?>
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script>
$(function() {
    $( ".DateFrom" ).datepicker({ changeMonth: true,changeYear: true,yearRange: "1877:2013",dateFormat: "mm-dd-yy" });
    $( ".DateTo"   ).datepicker({ changeMonth: true,changeYear: true,yearRange: "1877:2013",dateFormat: "mm-dd-yy" });
});
</script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<form method="post">
  <input type="text" class="DateFrom" name="f" />
  <input type="text" class="DateTo"   name="t" />
<input type="submit" />
</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
PeterErhardAuthor Commented:
Thank you , great example :)
0
Ray PaseurCommented:
Thanks for the points, ~Ray
0
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
MySQL Server

From novice to tech pro — start learning today.