AJAX日期未正确显示日期

时间:2017-03-08 10:59:03

标签: javascript php ajax datetime datepicker

我正在创建一个系统来预览系统上的事件,然后再提交并实际将它们保存到数据库中。

我正在使用AJAX调用来收集变量,处理它们,然后将它们连接到一个返回的变量并输入到Javascript中并显示在div中。我正在使用Bootstrap Datepicker和Bootstrap Timepicker来允许用户选择日期和时间,然后将这些一起附加到一个变量中。然后将其连接成返回要显示的HTML代码字符串。问题是显示的日期与我输入的日期完全没有关系。建议的修复程序将不胜感激。

Input

输出 01:33:37 - 01/01/70

Datepicker / Timepicker配置

$('#dteEStart').datepicker({
  autoclose: true,
  format: 'dd/mm/yyyy',
  startView: 'year'
});
$('#dteEEnd').datepicker({
  autoclose: true,
  format: 'dd/mm/yyyy',
  startView: 'year'
});
$('#dteESUDeadline').datepicker({
  autoclose: true,
  format: 'dd/mm/yyyy',
  startView: 'year'
});
$(".timepicker").timepicker({
  showInputs: false,
  minuteStep: 1
});

Javascript / AJAX致电

function UpdateEventPreview(){
    $('#divError').css('display', 'none');

    var StrETitle = $("txtETitle").val();
    var DteEStart = $("#dteEStart").val();
    var TimeEStart = $("#timeEStart").val();
    var DteEEnd = $("#dteEEnd").val();
    var TimeEEnd = $("#timeEEnd").val();
    var DteESUDeadline = $("#dteESUDeadline").val();
    var IntEMaleS = $("#numEMaleS").val();
    var IntEFemaleS = $("#numEFemaleS").val();
    var IntEPTag = $("#sltEPTag").val();

    var LstTags = [];
    for (var i = 0; i < $('#sltETags').select2('data').length; i++) {
        LstTags.push(Number($('#sltETags').select2('data')[i].id));
    }
    LstTags = JSON.stringify(LstTags);

    var StrEDesc = $('#txtEDesc').val();
    var StrEReq = $('#txtEReq').val();

    $.post('https://thomas-smyth.co.uk/functions/php/fncupdateeventpreview.php', {StrETitle: StrETitle, DteEStart: DteEStart, TimeEStart: TimeEStart, DteEEnd: DteEEnd, TimeEEnd, DteESUDeadline: DteESUDeadline, IntEMaleS: IntEMaleS, IntEFemaleS: IntEFemaleS, IntEPTag: IntEPTag, LstTags: LstTags, StrEDesc: StrEDesc, StrEReq: StrEReq}, function(data){
        if(data == 10){
            window.location.href = "https://thomas-smyth.co.uk/login";
        }
        else if (data == 11){
            $('#divError').css('display', '');
            $("#pError").text('HTML Injection Detected!');
        }
        else{
            $("#divEventPreview").html(data);
        }
    });
}

PHP

<?php
session_start();

require "tagHandler.php";
$TagManager = new tagHandler();

//Retrieves variables from Javascript.
$StrETitle = $_POST["StrETitle"];
$DteEStart = $_POST["DteEStart"];
$TimeEStart = $_POST["TimeEStart"];
$DteEEnd = $_POST["DteEEnd"];
$TimeEEnd = $_POST["TimeEEnd"];
$IntEMaleS = $_POST["IntEMaleS"];
$IntEFemaleS = $_POST["IntEFemaleS"];
$IntEPTag = $_POST["IntEPTag"];
$StrEDesc = $_POST["StrEDesc"];
$StrEReq = $_POST["StrEReq"];

$LstTags = array();
$LstTags = json_decode($_POST["LstTags"]);

if(!isset($_SESSION["LoginDetails"]) || $_SESSION["LoginDetails"][1] != "Staff" && $_SESSION["LoginDetails"][1] != "Developer"){
    $data = 10;
}
else if($StrETitle != strip_tags($StrETitle)){
    $data = 11;
}
else {
    $data = '<div class="box box-widget widget-user">
                        <div class="widget-user-header" style="background-color: #'.$TagManager->getTag($IntEPTag)[2].';">
                            <h3 class="widget-user-username">'.$StrETitle.'</h3>
                            <h5 class="widget-user-desc">'.$TagManager->getTag($IntEPTag)[1].'</h5>
                        </div>
                        <div class="widget-user-image">
                            <img class="img-circle" src="../dist/img/user2-160x160.jpg" alt="User Avatar">
                        </div>
                        <div class="box-footer">
                            <div class="row">
                                <div class="col-sm-4 border-right">
                                    <div class="description-block">
                                        <h5 class="description-header">Start Date</h5>
                                        <span class="description-text">'.date('H:i:s d/m/y', strtotime($DteEStart.' '.$TimeEStart)).'</span>
                                    </div>
                                </div>
                                <div class="col-sm-4 border-right">
                                    <div class="description-block">
                                        <h5 class="description-header">Sign Up Deadline</h5>
                                        <span class="description-text">'.date('Y-m-d', strtotime($_POST["DteESUDeadline"])).'</span>
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <div class="description-block">
                                        <h5 class="description-header">End Date</h5>
                                        <span class="description-text">'.date('H:i:s d/m/y', strtotime($DteEEnd.' '.$TimeEEnd)).'</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="box-header with-border" style="text-align: center;">
                                  <h3 class="box-title"><i class="fa fa-info-circle"></i> Description</h3>
                                </div>
                                <div class="box-body" style="text-align: center;">'.$StrEDesc.'</div>
                            </div>
                            <div class="col-md-6">
                                <div class="box-header with-border" style="text-align: center;">
                                  <h3 class="box-title"><i class="fa fa-exclamation-triangle"></i> Requirements</h3>
                                </div>
                                <div class="box-body" style="text-align: center;">'.$StrEReq.'</div>
                            </div>
                        </div>
                    </div>';
}
echo $data;
?>

更新 通过一些更多的测试,我相信我已经找到导致错误的原因。似乎日期混淆了彼此之间的月份和日期。因此,当日期大于12时,它会产生随机日期,因为它认为日期无效,但是当它在1-12之间时它会起作用。

1 个答案:

答案 0 :(得分:1)

您可以从旧版问题Strtotime() doesn't work with dd/mm/YYYY format中获取参考 实际问题在于php代码

<span class="description-text">'.date('H:i:s d/m/y', strtotime($DteEStart.' '.$TimeEStart)).'</span>

<强> FIX 在PHP

$DteEStart = $_POST["DteEStart"];
$DteEStart = str_replace('/', '-', $DteEStart);
echo date('H:i:s d/m/y', strtotime($DteEStart.' '.$TimeEStart));

或JS

$('#dteEStart').datepicker({
  autoclose: true,
  format: 'dd-mm-yyyy',/*change date formate*/
  startView: 'year'
});

您可以通过运行代码

来查看
<?php 
$DteEStart="22/02/2017";
$DteEStart = str_replace('/', '-', $DteEStart); /*see result with commenting and without commenting this line*/
$TimeEStart="10:45 AM";
$r=date('H:i:s d/m/y', strtotime($DteEStart.' '.$TimeEStart));
echo $r;
?>