PHP - 日期格式读取为m / j / Y而不是d / m / Y.

时间:2016-09-15 15:04:20

标签: php date post string-formatting date-format

我正在尝试使用PHP填充具有按日期列出的值的表。我将dateSelected变量的值设置为$ _POST,并将日期范围选择器设置为DD / MM / YYYY。以下是变量的设置。

   if ( isset($_POST['date'])){
$DateSelected = $_POST['date'];
}else {
unset($DateSelected);
$DateSelected = date('j/m/Y');
}

我可以显示表格的唯一时间是在控制器中调用函数期间翻转格式。如下 -

    GetHourlyPerformance(DateTime::createFromFormat('m/j/Y',
    $DateSelected)->format('j/m/Y')

显然,当我选择在该月12日之后有一天的行时。什么都不会退还。我为我的生活无法弄清楚我出错的地方或我应该在哪里格式化日期。因为它现在没有在控制器上重新格式化,它读作m / j / Y.

$ _POST变量的var_dump如下 -

    array(4) { ["workcenter"]=> string(2) "15" ["Week"]=> string(2) "37" ["date"]=> string(10) "11/09/2016" ["Shift"]=> string(1) "0" }

因此正确读取日期字符串,但表中没有显示任何内容。

    <?php
    function GetHourlySumary($date,$Workcenter, $Shift){
    try {
    $dbh = new PDO('sqlsrv:Server=etaddressOfTargt;     Database=CPB','$dbn','$db');

    $sql = "SELECT *  FROM HourlySummary WHERE Workcentre = (?) AND Shift = (?) AND  ProductionDate= (?) order by ProcessDate , ProcessHour" ;

    $stmt = $dbh->prepare($sql);


    $params = array($Workcenter,$Shift, $date);

    $stmt->execute($params);

    $cuRow = 0; 
    $toReturn = array(); 

    while( $row = $stmt->fetch() ) {

            $toReturn [$cuRow] ["Shift"] = $row['Shift'] ; 
            $toReturn [$cuRow] ["Hour"] =  $row['ProcessHour'] ; 
            $toReturn [$cuRow] ["Quantity Produced"] = $row['QuantityProduced'] ; 
            $toReturn [$cuRow] ["No WIPS"]  =  $row['ReelsProduced'] ; 
            $toReturn [$cuRow] ["Reams Produced"]=   $row['ReamsOut'] ; 
            $toReturn [$cuRow] ["Quantity Broke"]  =  $row['QuantityBroke'] ; 
            $toReturn [$cuRow] ["Target Broke"]  =  $row['TargetBroke'] ; 
            $toReturn [$cuRow] ["Mins Down"]  =  $row['MinutesDown'] ; 
        $cuRow++; 
    }



    $dbh = null;    
    return $toReturn;

} catch (PDOException $e) {
    print "Erreur !: " . $e->getMessage() . "<br/>";
    die();
}

}
?>

上面是模型的代码,下面是视图 - 其中某处$ dateSelected的值发生了变化。

    <div class="container">
    <form method= 'post' action= "" id = "form">
        <div class='row'>
            <div class='col-md-2'>
                Workcentre  : 
                <SELECT name="workcenter" class="form-control" onchange="this.form.submit()">
                    <?php for($i = 0; $i < sizeof($Workcenters);$i++)
                    {
                        if ($i == $WorkcenterSelected ) $selected = 'selected'; 
                        else $selected = '';
echo "<option value = {$i} {$selected}>{$Workcenters[$i]} </option>";}?>
                </SELECT>
            </div>
                            <div class='col-md-2'>
                Week  : 
                <SELECT name="Week" class="form-control" onchange="this.form.submit()">
                    <?php for($i = 1; $i <= date('W');$i++)
                    {
                        if ($i == $WeekSelected ) $selected = 'selected'; 
                        else $selected = '';
                        echo "<option value = {$i} {$selected}> Week {$i} </option>";
                    }
                    ?>
                </SELECT>
            </div>
            <div class='col-md-4'></div>
            <input type="text" id = "selectDate" name='date' class="form-control" style= "display: inline" value=<?php echo $DateSelected?> />

            <input type="text" id = "selectShift" name='Shift' class="form-control" style= "display: none" value=<?php echo $ShiftSelected?> />

            <div class='col-md-4'> 
                <?php if(isStatusUp($Workcenters[$WorkcenterSelected]))
                {
                    echo '<div class="alert alert-success" style="text-align:center;">
                    <strong>Status :</strong> Up
                    </div>'; 
                }else{
                    $Status = getStatus($Workcenters[$WorkcenterSelected]);
                    echo "<div class='alert alert-danger' style='text-align:center;'>
                        <strong>Status :</strong> ".$Status."
                    </div>"; 
                }
                ?>
            </div>
        </div>
    </form>
<br/>

<table class="table table-striped table-bordered">
   <tr>
       <th>Shift</th>
       <th>QTY Produced</th>
       <th>No. of WIPs</th>
       <th>Reams Produced</th>
       <th>QTY Broke</th>
       <th>Target Broke</th>
       <th>Mins Down</th>

   </tr>
   <?php 

   foreach ($ShiftSumaryTab as &$row)
   {
        $date = DateTime::createFromFormat('Y-m-d', $row['Production Date']);
        echo
        "<tr class='table-row' data-date={$date->format('d/m/Y')} data-shift ={$row['Shift']} >
                <td>{$row['Shift']} {$date->format('D M-d')} </td>
                <td>{$row['Quantity Produced']}</td>
                <td>{$row['No WIPS']}</td>
                <td>{$row['Reams Produced']}</td>
                <td>{$row['Quantity Broke']}</td>
                <td>{$row['Target Broke']}</td>
                <td>{$row['Minutes Down']}</td>

            </tr>";
   }
    echo
        "
        <tr>
            <td bgcolor='#b3d9ff'><strong> Week : {$WeekSelected} </strong></td>
            <td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Quantity Produced']}</strong></td>
            <td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['No WIPS']}</strong></td>
            <td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Reams Produced']}</strong></td>
            <td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Quantity Broke']}</strong></td>
            <td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Target Broke']}</strong></td>
            <td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Minutes Down']}</strong></td>

        </tr>";
   ?>
</table>
</div>
<script type="text/javascript">


$(function() {
$(".table-row").click(function() {
    $("#form").attr('action', '?action=HS');
    $("#selectDate").val($(this).data("date"));
    $("#selectShift").val($(this).data("Shift"));
    $("#form").submit();
});
});
</script>

1 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,在执行查询时格式错误,将格式更改为读取Y-m-d并且我们拥有它。

$HourlyPerformanceTab = 
GetHourlyPerformance(DateTime::createFromFormat('d-m-Y',$DateSelected)->format('Y-m-d'),
$Workcenters[$WorkcenterSelected], $Hours[ $HourSelected] );