yuki
yuki

Reputation: 43

multiple datepicker in a page (php)

im trying to use two date picker in a page. it seems to worked fine but after clicked on the submit button the date entered is not the same as the choosen date. both of the date inserted to the database is 01-01-1970.

how can i fix this?

<div class="form-group">
    <label class="control-label col-sm-4">Date Issued</label>
    <div class="col-sm-5">
        <input class="form-control" type="text" id="dateIssuedpc" name="dateissued" required class="form-control" placeholder="Enter Date Issued"/>
    </div>
</div>

<div class="form-group">
    <label class="control-label col-sm-4">Date Expired</label>
    <div class="col-sm-5">
    <input class="form-control" type="text" id="dateExpiredpc" name="dateexpired" required class="form-control"  placeholder="Enter Date Expired" />
    </div>
</div>



<script>
$( function() {
$( "#dateIssuedpc").datepicker({
  dateFormat: "dd-mm-yy",
  changeMonth: true,
  changeYear: true
});
$( "#dateExpiredpc" ).datepicker({
  dateFormat: "dd-mm-yy",
  changeMonth: true,
  changeYear: true
  });
  } );
 </script>

Upvotes: 0

Views: 1247

Answers (3)

Kruti Aparnathi
Kruti Aparnathi

Reputation: 175

if field data type in database is datetime then

$dateexpired = $_POST['dateexpired'];
$new_dateexpired = date("Y-m-d H:i:s",strtotime($dateexpired));
$dateissued = $_POST['dateissued'];
$new_dateissued = date("Y-m-d H:i:s",strtotime($dateissued));

if field data type is date then

$dateexpired = $_POST['dateexpired'];
$new_dateexpired = date("Y-m-d",strtotime($dateexpired));
$dateissued = $_POST['dateissued'];
$new_dateissued = date("Y-m-d",strtotime($dateissued));

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

This is not datepicker problem, rather problem is with PHP code.
Mysql date format is always YYYY-MM-DD HH:II:SS

So while inserting data to mysql table, you have to convert the format for mysql

Ex:

$postDate = $_POST['date'];// just collect value from post fields.
$date = date("Y-m-d H:i:s",strtotime($postDate));

Upvotes: 1

Parag Soni
Parag Soni

Reputation: 713

You need to format your date properly. First of all change format from dateFormat: "dd-mm-yy", to *dateFormat: "dd-mm-yyyy",* to better recognise year.

After that from php side you something like this

$dateissued = date("Y-m-d", strtotime($_POST['dateissued']) ); //MySql date format

Upvotes: 0

Related Questions