PNewCode Posted January 15, 2023 Share Posted January 15, 2023 Hello. Can someone help me with what I need to add to the following line of code to make it echo as Jan 6 2023 11:59pm instead of 2023-01-06 21:59:22 The column in the database is named "created_at" which is filled in when a user makes an account on my website. The issue is that I cannot change the way the date enters the database when a user signs up for an account. So I have to translate it on the page to view this for this line. Here is the line of code I have. <td><center><b><font color="green">'.$row["created_at"].'</font></b></td> Quote Link to comment https://forums.phpfreaks.com/topic/315806-converting-time-from-format-in-database/ Share on other sites More sharing options...
Solution Barand Posted January 15, 2023 Solution Share Posted January 15, 2023 You can do it in your query with SELECT date_format(created_at, '%b %e %Y %h:%i%p'), ... or you can do it in php with date('M j Y g:ia', strtotime($row['created_at'])) Quote Link to comment https://forums.phpfreaks.com/topic/315806-converting-time-from-format-in-database/#findComment-1604706 Share on other sites More sharing options...
PNewCode Posted January 15, 2023 Author Share Posted January 15, 2023 @Barand Thank you. You rock! Quote Link to comment https://forums.phpfreaks.com/topic/315806-converting-time-from-format-in-database/#findComment-1604708 Share on other sites More sharing options...
PNewCode Posted January 16, 2023 Author Share Posted January 16, 2023 (edited) @Barand I felt for sure you gave me the right answer, and you probably did. However I seem to have this wrong somehow. When I use the following, I echo blank spaces and gives the error Warning: Undefined array key "created_at" in /home/vbauyvmy/public_html/peredy1/modshit/phtokens4.php on line 137 which is <td><center><b><font color="green">'.$row["created_at"].'</font></b></td> This is what I have in the query $sql = "SELECT id,unique_id,fname,email,token,movie,movie2,date_format(created_at, '%b %e %Y %h:%i%p') FROM users ORDER BY fname"; (the second suggestion to put in that string gave me an undefined error on the whole page) date('M j Y g:ia', strtotime($row['created_at'])) Edited January 16, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/315806-converting-time-from-format-in-database/#findComment-1604712 Share on other sites More sharing options...
Barand Posted January 16, 2023 Share Posted January 16, 2023 Give the formatted date a column alias SELECT date_format(created_at, '%b %e %Y %h:%i%p') as created_at Without the alias you need to use $row["date_format(created_at, '%b %e %Y %h:%i%p')"] Quote Link to comment https://forums.phpfreaks.com/topic/315806-converting-time-from-format-in-database/#findComment-1604713 Share on other sites More sharing options...
PNewCode Posted January 16, 2023 Author Share Posted January 16, 2023 @Barand You're a genius. Thank you so much!!! And I learned quite a good exciting bit from this too. Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/315806-converting-time-from-format-in-database/#findComment-1604714 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.