0

我有一个 SQL 查询,它经过搜索并显示数列的数百条记录。但是必须更改两列的显示;生效日期和清除日期。目前,它们都显示如下:(例如 1991 年 8 月 29 日)。我想将输出更改为 1991 年 8 月 29 日。我不确定我会如何做到这一点,因为它将这些数据直接从数据库中提取到表中。
请注意我在这个项目中使用 MS-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 社区的新手。我一直在研究中使用这个网站,但第一次创建了一个帐户。

4

1 回答 1

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>";
} 
于 2013-08-27T15:47:12.310 回答