2017-03-08 187 views
0

我正在創建一個系統在提交之前在我的系統上預覽事件並將它們實際保存到數據庫。AJAX日期不正確顯示日期

我正在使用AJAX調用收集變量,處理它們,然後將它們連接到一個變量,該變量返回並輸入到Javascript中並顯示在div中。我使用Bootstrap Datepicker和Bootstrap Timepicker來允許用戶選擇日期和時間,然後將它們一起附加到一個變量中。然後將其連接成返回顯示的HTML代碼字符串。問題是顯示的日期與我輸入的內容完全沒有關係。建議的修復將不勝感激。

Input

輸出 一時33分37秒 - 70年1月1日

日期選擇器/ 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之間時,它會起作用。

+0

請問您可以告訴您輸入的是什麼? –

+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; 
?>