1

我正在学习 PHP,并且有一段查询数据库的 PHP 代码。我获得了一个已经在数据库中的日期,并且想知道这段代码format('dd-mm-yyyy');是否可以将数据从默认的 'YYYY-MM-DD' 格式化为 'DD-MM-YYYY' 以及如何使用该代码?

<?php 

    $rfp = $_GET['cid'];
 // Connects to Database 
 mysql_connect("localhost", "root") or die(mysql_error()); 
 mysql_select_db("test") or die(mysql_error()); 
 $data = mysql_query("SELECT rfp_id, issue_date, rfp_status.status FROM company, rfp, rfp_status WHERE company.company_id = rfp.company_id AND rfp_status.status_id = rfp.status_id AND company.company_id = '$rfp'") 
 or die(mysql_error()); 
  echo "<table border=1px cellpadding=15 bordercolor='#0000CC'>";
   #echo "<table border=0 cellpadding=15 bordercolor='#0000CC'>";
echo "<tr align = center bgcolor=white>
<td><b>RFP ID</b></td><td><b>Date Added</b></td><td><b>Status</b></td>" ; 
 while($row = mysql_fetch_array($data)){
      $rid = $row['rfp_id'];  
      $idate = $row['issue_date'];
      $status = $row['status'];

# inserts value into table as a hyperlink

echo "<tr align = center bgcolor=white><td>$rid</td><td><b><a target='_blank' href=view_section_detail.php?rid=$rid>$idate</a></b></td><td>$status</td>";
}

# displays table

print '</table>';

 ?>

任何帮助,将不胜感激

谢谢

4

5 回答 5

3

在 MySQL 中,您可以使用DATE_FORMAT()

SELECT DATE_FORMAT(colName, '%d-%m-%Y') formattedDate
FROM...
WHERE ...

其他格式在这里

SQLFiddle 演示

于 2012-09-17T14:23:16.757 回答
2

没有format()

$idate = strtotime($row['issue_date']); // assuming $row['issue_date'] is YYYY-MM-DD
$idate = date('d-m-Y', $idate);
于 2012-09-17T14:23:10.663 回答
2

您可以使用 mysql date_format直接在数据库查询中执行此操作:

SELECT rfp_id, date_format(issue_date,'%d-%m-%Y') as issue_date... etc
于 2012-09-17T14:23:18.487 回答
0

尝试这个

while($row = mysql_fetch_array($data)){
      $rid = $row['rfp_id'];  
      $idate = date("d-m-Y", strtotime($row['issue_date']));
      $status = $row['status'];

# inserts value into table as a hyperlink

echo "<tr align = center bgcolor=white><td>$rid</td><td><b><a target='_blank' href=view_section_detail.php?rid=$rid>$idate</a></b></td><td>$status</td>";
}
于 2012-09-17T14:27:39.470 回答
0

尝试这个

$idate = date('d-m-Y',strtotime($row['issue_date']));
于 2012-09-17T14:24:55.140 回答