【问题标题】:jQuery UI datepicker: Displaying 1969 although I use strtotimejQuery UI datepicker:虽然我使用 strtotime,但显示 1969
【发布时间】:2012-04-21 04:45:10
【问题描述】:

我有 PHP+JQUERY 房间预订应用程序。我正在使用 datepicker 小部件来选择日期,但遇到了问题。我尝试将所选日期(以 dd/mm/yyyy 格式)转换为 YYYY-mm-dd 格式,以便将其插入到我的数据库中。当我选择第一个日期时,它转换得很好,但是当我选择其他日期时,我看到日期 1969-12-31。这是我的 JQUERY 代码:

    $(function() {
    $( "#datepicker" ).datepicker({ 
        showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,          
        minDate: 0, 
        maxDate: "+3W", 
        dateFormat: "dd/mm/yy",
        beforeShowDay: function (date) {
        var day = date.getDay();
        return [(day == 0 || day == 1 || day == 2 || day == 3 || day == 4), ''];
        },
        onSelect: function(dateText) {
            $("#registration").load("room.php #registration", {selectedDate: dateText}, function() { 
                $( "input:submit, a, button", ".registration" ).button();
                $( "a", ".registration" ).click(function() { return false; });
            });

        }

    });
    });  

然后我回显测试结果:

    <?php if(isset($_POST['selectedDate']))
           { 
              $selectedDate=$_POST['selectedDate']; 
              echo date('Y-m-d',strtotime((string)$selectedDate)); 
           } 
    ?>

这是我应用中的一张图片:http://oi43.tinypic.com/29tv2c.jpg

1:

【问题讨论】:

    标签: php jquery datepicker


    【解决方案1】:

    最终我通过了这个问题。我没有在 PHP 中转换它,而是在 JS 中转换它:

            onSelect: function(dateText) {
    
            //Converting the date format by spliting the date.
            var dt= dateText;
            var arrDt = dt.split('/');
            var newDt = arrDt[2] + "-" + arrDt[1] + "-" + arrDt[0];
    
            //Loading the rooms div acoording to the sent selected date in JSON format  
                $("#registration").load("room.php #registration", {selectedDate: newDt}, function() { 
                    $( "input:submit, a, button", ".registration" ).button();
                    $( "a", ".registration" ).click(function() { return false; });
                });
    
            }
    

    但是我还是不知道是什么导致了date()函数转换的问题!

    【讨论】:

      【解决方案2】:

      您在 MySQL 中存储的选定日期的类型是什么?对于 DATE 数据类型,您只需将 'YYYY-MM-DD 格式传递给 INSERT 语句。

      详情请见this question

      还有this http://dev.mysql.com/doc/refman/5.5/en/datetime.html

      【讨论】:

      • 我不会尝试将其传递给 MYSQL,我只是在 php 中回显转换以用于测试目的。选定的日期来自 jquery datepicker,同时我要做的就是用 PHP 中的日期函数转换它的格式。我只是不明白为什么它会部分工作!
      • @user1293194 因为 strtotime 函数将字符串格式转换为表示从 1970 年 1 月 1 日(UNIX EPOCH 时间)开始的秒数的 int 数字。这样您就无法在 1970 年 1 月 1 日之前存储价值。
      猜你喜欢
      • 2022-12-11
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多