简体   繁体   中英

mysql dateformat with jquery date format not working out

I have a mypage.php and it contains the below code

And i have added the

<link style="text/css" href="http://jqueryui.com/themes/base/jquery.ui.all.css" rel="stylesheet">

<script type="text/javascript" src="/scripts/jquery.js"></script>

<script type="text/javascript" src="/scripts/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="/scripts/ui/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/scripts/ui/jquery.ui.datepicker.js"></script>

<form name="test_date" method="post">
    <input type="text" id="datepicker1" name="date1">
    <input type="submit" name="insert" value="Insert and show">
</form>

And In jquery.ui.datepicker.js i have change the date format to dateFormat: 'dd/mm/yy' from dateFormat: 'mm/dd/yy'

and below is the jquery for selecting the date when click on textbox

<script>
    $(function() {
        $( "#datepicker1" ).datepicker();
    });
</script>

And when i click on submit button i have inserted into mysql database database

<?php
if($_POST['insert']) {
echo "Date : ";
$d = $_POST['date1'];

require('con_open.php');

$t = date('Y-m-j H:i:s', strtotime($d));
mysql_query("insert into date_table (date_field1) values ('$t')");

echo "Converted to Date at Runtime :" . $t;
echo "<br>";
$query = mysql_query("select date_field1 from date_table");
while($r = mysql_fetch_row($query))
{
    got_date_db = $r[0];
}
echo "Displaying date from Database : " . got_date_db;

require('con_close.php');
}
?>

Output :

Converted to Date at Runtime : 2011-12-28 14:32:16

Displaying date from Database : 0000-00-00 00:00:00

date_field1 (DATETIME) fromat on mysql

Even i have tried out checking with dateformat of mysql and php date function manuals online and over stackoverflow... but i could not get any solution to this one.

Try:

mysql_query("
  insert into date_table (date_field1)
  values (FROM_UNIXTIME(". (int)strtotime($_POST['date1']) ."))
");

I think its generally a good idea to pass a date as a timestamp to the database. Then you don't have to worry about formatting it the right way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM