Search code examples
phpmysqlcodeigniterdatejquery-ui-datepicker

CodeIgniter Date or DateTime


I have one problem, which I can not resolve and have no idea where is mistake One I create a tabel called posts and in codeigniter create a form Posts, after createing all post I want to display posts in index.php and I have table called Date of departure, and date of return. One i create post date looking good but when I display in my index.php it's look like 0000-00-00 00:00:00 In begginig data type was Date, now i change to be DateTime but nothing happend, same problem Maybe I make mistake in my JS script Any comment ?

My script

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

Solution

  • When your field in MySQL is Date-time it aspects proper format before saving in database.

    So convert string to date time format in php using.

    date("Y-m-d H:i:s", strtotime($string)); 
    

    Or you can do the same in Datepicker

    $(function() {
        $('#datepicker').datepicker({
            dateFormat: 'yy-dd-mm',
            onSelect: function(datetext){
                var d = new Date(); // for now
                var h = d.getHours();
                    h = (h < 10) ? ("0" + h) : h ;
    
                    var m = d.getMinutes();
                m = (m < 10) ? ("0" + m) : m ;
    
                var s = d.getSeconds();
                s = (s < 10) ? ("0" + s) : s ;
    
                    datetext = datetext + " " + h + ":" + m + ":" + s;
                $('#datepicker').val(datetext);
            },
        });
    });
    

    Fiddle for datepicker

    Note if you want date only in MySQL field the omit hour min sec part of conversion