2013-08-27 59 views
0

我有一個SQL查詢經過搜索並顯示數百列的記錄。但是兩列的顯示必須改變;生效日期和清除日期。目前它們都是這樣顯示的:(例如1991年8月29日)。我想將輸出更改爲1991年8月29日。我不確定我會怎麼做,因爲它將數據直接從數據庫中提取到表中。
請注意我在這個項目中使用MS-SQL和PHP。從SQL查詢更改PHP頁面上的顯示

這是我的SQL查詢:

$sql = "select SerialNum as [Serial Number],ts_sitename As Site,(case m.Scratched 
    when 0 then 'Live' 
    when 1 then 'Free' 
    END) as Status, Note as Comment, (case Destroyed when 0 then 'NO' 
    when 1 then 'YES' END) as [Destroyed], 
    SUBSTRING(cast(mg_effectivedate as char), 1, 12) AS [Effective Date], 
    SUBSTRING(cast(mg_effectivedate as char), 12, 8) AS [Effective Time], 
    SUBSTRING(cast(ScratchedDate as char), 1, 4) + '-' + 
    SUBSTRING(cast(ScratchedDate as char), 3, 2) + '-' + 
    SUBSTRING(cast(ScratchedDate as char), 5, 2) 
    AS [Scratched Date], 
    SUBSTRING(cast(ScratchedDate as char), 10, 2) + ':' + 
    SUBSTRING(cast(ScratchedDate as char), 12, 2) + ':' + 
    SUBSTRING(cast(ScratchedDate as char), 14, 2) 
    AS [Scratched Time], 
    SUBSTRING(cast(mg_scratchdate as char), 1, 12) AS [Purge Date], 
    (select fl_filename from TheFiles_tab where mg_filenum = fl_filenum) as [Dataset], 
    (select hs_hostname from TheHosts_tab where mg_hostnum = hs_hostnum) as [Host], 
    (select UserCode from [User] where mg_usernum = UserId) as [UserCode] 
    from ((Media m left join MediaGenT g on m.MediaId = g.mg_medianum) 
    join TheSites_tab s on m.SiteId = s.ts_sitenum) 
    join Note n on m.NoteId = n.NoteId 
    where SerialNum like '" . $userQuery . "%'"; 

這我的代碼顯示兩列:

echo '<th>Effective Date</th>'; 
    echo '<th>Purge Date</th>'; 

    foreach ($result as $r) 
    { 
    echo "<tr>"; 
    echo "<td>".$r['Serial Number'] . " </td>"; 
    echo "<td>".$r['Site'] . " </td>"; 
    echo "<td>".$r['Status'] . "</td> "; 
    echo "<td>".$r['Comment'] . " </td>"; 
    echo "<td>".$r['Destroyed'] . " </td>"; 
    echo "<td>".$r['Effective Date'] . " </td>"; 
    echo "<td>".$r['Effective Time'] . "</td> "; 

    echo "<td>"; 
    if ($r['Scratched Date'] == "") { 
    echo "NULL"; 
    } 
    else { 
    echo $r['Scratched Date']; 
    } echo "</td>"; 

    echo "<td>" ; 
    if ($r['Scratched Time'] == "") { 
    echo "NULL"; 
    } 
    else { 
    echo $r['Scratched Time']; 
    } echo "</td>"; 


    echo "<td>".$r['Purge Date'] . " </td>"; 
    echo "<td>".$r['Dataset'] . "</td> "; 
    echo "<td>".$r['Host'] . " </td>"; 
    echo "<td>".$r['UserCode'] . " </td>"; 
    echo "<td>".$r['NoteId'] . " </td>"; 
    echo "</tr>"; 
    } 

請注意,我是新來的SO社區。我一直使用這個網站進行研究,但第一次剛剛創建了一個帳戶。

+0

你可以使用像'名單($個月,$日,$年)=爆炸($ R [ '生效日']) ;'然後只是'echo(「$ day- $ month- $ year」);'。這是你想要的? – Technoh

+0

@technoh會改變所有記錄的顯示嗎? – user2722215

回答

0

如果我理解正確,你只是想替換連字符日期中的空格?

現在您的日期格式: MMM DD YYYY

你想:如果我沒有記錯 DD-MMM-YYYY

,在PHP中,你可以這樣做:

$effective_date = date("d-M-Y", strtotime($r['Effective Date'])); 

那將數據庫提取的值轉換爲時間戳,然後將該時間戳格式化爲新的日期符號。查看php的date()函數,瞭解您有哪些其他選項:PHP: date - Manual

您也可以更改其他日期。

編輯

總PHP代碼:

echo '<th>Effective Date</th>'; 
echo '<th>Purge Date</th>'; 

foreach ($result as $r) 
{ 
    echo "<tr>"; 
    echo "<td>".$r['Serial Number'] . " </td>"; 
    echo "<td>".$r['Site'] . " </td>"; 
    echo "<td>".$r['Status'] . "</td> "; 
    echo "<td>".$r['Comment'] . " </td>"; 
    echo "<td>".$r['Destroyed'] . " </td>"; 
    echo "<td>".date("d-M-Y", strtotime($r['Effective Date'])) . " </td>"; 
    echo "<td>".$r['Effective Time'] . "</td> "; 

    echo "<td>"; 
    if ($r['Scratched Date'] == "") { 
    echo "NULL"; 
    } 
    else { 
    echo $r['Scratched Date']; 
    } echo "</td>"; 

    echo "<td>" ; 
    if ($r['Scratched Time'] == "") { 
    echo "NULL"; 
    } 
    else { 
    echo $r['Scratched Time']; 
    } echo "</td>"; 


    echo "<td>".date("d-M-Y", strtotime($r['Purge Date'])) . " </td>"; 
    echo "<td>".$r['Dataset'] . "</td> "; 
    echo "<td>".$r['Host'] . " </td>"; 
    echo "<td>".$r['UserCode'] . " </td>"; 
    echo "<td>".$r['NoteId'] . " </td>"; 
    echo "</tr>"; 
} 
+0

那麼我會在我的文件中添加那行代碼?我會用它替換一個td或th標籤嗎? – user2722215

+0

允許我編輯我的文章,我將爲您發佈完整的PHP代碼,1秒。 – Floris

+0

謝謝,我明白這一點。 – user2722215