vivification Posted December 9, 2013 Share Posted December 9, 2013 Hi there, I am still new to PHP and having trouble trying to format a date. When the form data is saved; it is saved as YYYY-MM-DD. Then, when you view a page to display all the records, it shows as YYYY-MM-DD. I want to convert it to show DD-MM-YYYY. The field name is "ETD" and "ETA". Can I just use: echo date("Y/m/d") ? If so, how do I use it in the below scenario. Would it be: echo "<td>" . $row['etd']date("Y/m/d") . "</td>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><a href='edit--report.php?id=" . $row['ID'] . "' >" . $row['ID'] . "</a></td>"; echo "<td>" . $row['etd'] . "</td>"; echo "<td>" . $row['eta'] . "</td>"; echo "</tr>"; } echo "</table>"; //mysqli_close($con); ?> Would appreciate any help! Thanks, Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 9, 2013 Share Posted December 9, 2013 (edited) use the DateTime object. $date = new DateTime($row['etd']); print $date->format('d-m-Y'); http://php.net/datetime.format Edited to change format from "Y-m-d" to "d-m-Y". Edited December 9, 2013 by hitman6003 Quote Link to comment Share on other sites More sharing options...
vivification Posted December 9, 2013 Author Share Posted December 9, 2013 Thanks hitman, but can you show me how I enter it on my code? I dont understand how I need to enter/display it in my code. Are they both on the same line? i.e. while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><a href='edit--report.php?id=" . $row['ID'] . "' >" . $row['ID'] . "</a></td>"; echo "<td>" . $date = new DateTime($row['etd']); print $date->format('d-m-Y') "</td>"; echo "</tr>"; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
Solution aysiu Posted December 9, 2013 Solution Share Posted December 9, 2013 Another way to do it may be something like this: echo '<td>' . date("d-m-Y", strtotime($row['etd'])) . '</td>'; Quote Link to comment Share on other sites More sharing options...
vivification Posted December 9, 2013 Author Share Posted December 9, 2013 Thank you so much aysiu!! that's perfect. Thank you also hitman. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.