问题
We are fetching values of column "due_date" & displaying in site.
Its working fine with help of below code.
$i = 0;
foreach($order as $orderData)
{
$k = 0;
while ($k < count($orderitemsarray)) {
if ($orderitemsarray[$k] != '0') {
if($accountType == "admin") {
$due_date='';
while($datas = $stmt1->fetch()) {
$due_date=$datas['due_date'];
$oDate1 = new DateTime($datas['due_date']);
$sDate1 = $oDate1->format("M d, Y");
}
$responce[] = array( $due_date );
}
return json_encode($responce);
script
var colsOption = [
{id: 'due_date' , header: " Due Date" , width :"140"},
];
I want to display date in this format :
so i tried below code
$responce[] = array(
$sDate1
);
Now its displaying Date for other rows also, but for those other rows there is no values [ Due date ] in Database.
回答1:
In your code you your are adding values in $response[] using loop. What happens is that value of $sDate1 is not initialized to null. So when it is set for first time it does not change until next due_date value comes so it keeps on repeating last $sDate1 value to solve this , Go to line 152 in this file http://pastebin.com/PnPnX9Wiand bellow it initialize $sDate1. Make changes in this code.
$paid_status='';
$delivery_status='';
$due_date='';
Add $sDate1='';. It will look like this.
$paid_status='';
$delivery_status='';
$due_date='';
$sDate1='';
Now change this code.
$due_date=$datas['due_date'];
$oDate1 = new DateTime($datas['due_date']);
$sDate1 = $oDate1->format("M d, Y");
with this code.
$oDate1 = new DateTime($datas['due_date']);
$due_date = $oDate1->format("M d, Y");
$sDate1 = $oDate1->format("M d, Y");
回答2:
You need to apply check for empty date
while($datas = $stmt1->fetch()) {
$due_date=$datas['due_date'];
if(isset($due_date)){ // Apply the check for empty if empty dont pass it to Datetime function.
$oDate1 = new DateTime($datas['due_date']);
$sDate1 = $oDate1->format("M d, Y");
$responce[] = $sDate1;
}
}`
回答3:
Updated the code after reading the pastebin (see comments below).
$sDate1 = '';
while($datas = $stmt1->fetch()) {
if ($datas['due_date'] != null) {
$oDate1 = new DateTime($datas['due_date']);
$sDate1 = $oDate1->format("M d, Y");
}
}
$responce[] = array( $sDate1 );
回答4:
Instead of this part...
while($datas = $stmt1->fetch()) {
$due_date=$datas['due_date'];
$oDate1 = new DateTime($datas['due_date']);
$sDate1 = $oDate1->format("M d, Y");
}
$responce[] = array( $due_date );
}
return json_encode($responce);
Try something like this...
$response = array();
...
$i = 0;
foreach($order as $orderData)
{
$k = 0;
while ($k < count($orderitemsarray)) {
if ($orderitemsarray[$k] != '0') {
if($accountType == "admin") {
while($datas = $stmt1->fetch()) {
if(!empty($datas['due_date'])){
// place the Datetime part here... in case $datas['due_date'] is empty*
$oDate1 = new DateTime($datas['due_date']);
$due_date = $oDate1->format("M d, Y");
} else {
$due_date = "";
}
}
...
...
return json_encode($responce);
...
(*) check the output of Datetime here if $datas['due_date']
is "" or null
回答5:
You need to check variable is empty or not and then format the date this the updated code
$i = 0;
foreach($order as $orderData)
{
$k = 0;
while ($k < count($orderitemsarray)) {
if ($orderitemsarray[$k] != '0') {
if($accountType == "admin") {
$due_date='';
while($datas = $stmt1->fetch()) {
$due_date=$datas['due_date'];
$oDate1 = new DateTime($datas['due_date']);
if (!empty($oDate1))
$sDate1 = $oDate1->format("M d, Y");
}
$responce[] = array( $due_date );
}
return json_encode($responce);
来源:https://stackoverflow.com/questions/41238427/converting-date-in-m-d-y-format-leads-to-display-date-in-other-rows