CodeIgniter Date or DateTime

穿精又带淫゛_ 提交于 2019-12-11 06:25:40

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/45780947/codeigniter-date-or-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!