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, Link to comment https://forums.phpfreaks.com/topic/284633-how-to-display-date-in-dd-mm-yyyy/ Share on other sites More sharing options...
hitman6003 Posted December 9, 2013 Share Posted December 9, 2013 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". Link to comment https://forums.phpfreaks.com/topic/284633-how-to-display-date-in-dd-mm-yyyy/#findComment-1461710 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>"; Link to comment https://forums.phpfreaks.com/topic/284633-how-to-display-date-in-dd-mm-yyyy/#findComment-1461712 Share on other sites More sharing options...
aysiu Posted December 9, 2013 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>'; Link to comment https://forums.phpfreaks.com/topic/284633-how-to-display-date-in-dd-mm-yyyy/#findComment-1461713 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. Link to comment https://forums.phpfreaks.com/topic/284633-how-to-display-date-in-dd-mm-yyyy/#findComment-1461715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.