问题
I have a problem and I can't resolve it, So my script:
<script>
$(document).ready(function() {
$("#datepicker").datepicker({
format: 'dd-mm-yy'
});
})
My html:
<div class="form-group">
<label>Date:</label>
<input class="form-control marg-left-10" name="date" type="text" id="datepicker">
</div>
My php:
$date = date('dd-mm-yy', strtotime($this->input->post['date']));
But the date doesn't insert, in my database the date look like this : 0000-00-00
Help me please
回答1:
Use the appropriate format
//converting one date format into another
$date = date('Y-m-d', strtotime($this->input->post['date']));
回答2:
MySQL database supports YYYY-mm-dd date format
So you should change your script...
<script>
$(function(){
var pickerOpts = {
dateFormat: "yy-mm-dd"
};
$("#datepicker").datepicker(pickerOpts);
});
</script>
回答3:
As You Stated
My php:
$date = date('dd-mm-yy', strtotime($this->input->post['date']));
But the date doesn't insert, in my database the date look like this : 0000-00-00
Thing is that you are taking the date in dd-mm-YYYY format while on myswl it is saved as YYYY-mm-dd format
SO its better either you take a colum as varchar and save the date as 'dd-mm-YYYY' as string
or if you want to use mysql date format then covert your date into YYYY-mm-dd format
which can be done like this
$old_date = $this->input->post['date'];
$o_date_time = strtotime($o_date);
$date = date('Y-m-d', $o_date_time);
回答4:
MYSQL Support Date Format to save 'YYYY-MM-DD' or 'YY-MM-DD'
As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html
Change to your code like this
$date = date('Y-m-d', strtotime($this->input->post['date']));
OR
$date = new DateTime($this->input->post['date']);
echo $date->format('Y-m-d');
回答5:
First change your formate to this:
$date1=date('Y-m-d',strtotime($fromd));
And if you want to change the format then do something like this on the page where you want to show the date..
<?php
$dte=date_create($rows->database row);
echo date_format($dte,'d/m/y');
?>
回答6:
Mysql uses yyyy-mm-dd format so firs tyou have to change your date format and then change the datatype of your date column to text
来源:https://stackoverflow.com/questions/25482581/insert-data-from-datepicker-in-database-using-php