i am learning PHP, and have a piece of PHP code that queries a Database. I obtain a date that is already in the Database and was wondering whether this piece of code format('dd-mm-yyyy');
would work to format the data from the default 'YYYY-MM-DD' to 'DD-MM-YYYY' and how the code can be utilsed?
<?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>';
?>
Any help would be appreciated
Thanks
You can do it directly in the database query like this using mysql date_format:
SELECT rfp_id, date_format(issue_date,'%d-%m-%Y') as issue_date... etc
There is no format()
:
$idate = strtotime($row['issue_date']); // assuming $row['issue_date'] is YYYY-MM-DD
$idate = date('d-m-Y', $idate);
In MySQL, you can use DATE_FORMAT()
SELECT DATE_FORMAT(colName, '%d-%m-%Y') formattedDate
FROM...
WHERE ...
try this
$idate = date('d-m-Y',strtotime($row['issue_date']));
Try this
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>";
}