chaseman Posted December 27, 2010 Share Posted December 27, 2010 What I'm basically trying to accomplish is, that the datetime of the user posted information is registered with the Now() function in the query, BUT that the user entered information gets echo'd out with the Date showing ONLY. What I have is this: -Just as an example- INSERT INTO db (date_created) VALUES (now()) while ($row = mysqli_fetch_array($data)) { echo '<tr><td>' . $row['date_created'] . '</td></tr>'; } mysqli_close($dbc); With this way of course I get Date and Time showcased just like it's inserted into the DB. What would be a legit way of showcasing just date. Thanks for help. EDIT: or is there a better way than using now() to showcase the date when the user entered information was posted? Quote Link to comment https://forums.phpfreaks.com/topic/222724-insert-into-db-datetime-and-echo-out-only-date/ Share on other sites More sharing options...
revraz Posted December 27, 2010 Share Posted December 27, 2010 http://php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/222724-insert-into-db-datetime-and-echo-out-only-date/#findComment-1151760 Share on other sites More sharing options...
PFMaBiSmAd Posted December 27, 2010 Share Posted December 27, 2010 To just get the date part of a datetime value when you select the data, use the mysql DATE() function in your query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date However, since you are probably formatting the date into some format other than YYYY-MM-DD, use the mysql DATE_FORMAT() function in your query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format No slow parsed, tokenized, interpreted php code is needed. Doing this in your query is at least 8 times faster than using php code. Quote Link to comment https://forums.phpfreaks.com/topic/222724-insert-into-db-datetime-and-echo-out-only-date/#findComment-1151766 Share on other sites More sharing options...
chaseman Posted December 27, 2010 Author Share Posted December 27, 2010 Not sure if I understand this clearly. Using the date() function in the query would also mean that the date gets inserted into the Database WITHOUT the time right? That's not what I want tho. I want it to be inserted with the time, BUT echo'd out WITHOUT the time... Is there not a function where I just can take the datetime from the database and leave out the time when echo'ing out? Quote Link to comment https://forums.phpfreaks.com/topic/222724-insert-into-db-datetime-and-echo-out-only-date/#findComment-1151768 Share on other sites More sharing options...
mmarif4u Posted December 27, 2010 Share Posted December 27, 2010 You can do this in query as well in PHP. select date(datetime_field) ........... select DATE_FORMAT(timestamp,'%M %D, %Y') Many more... In PHP: Assume, datetime format is 2010-12-27 12:23:12. $date = explode(' ', $datetime); echo $date[0]; Quote Link to comment https://forums.phpfreaks.com/topic/222724-insert-into-db-datetime-and-echo-out-only-date/#findComment-1151776 Share on other sites More sharing options...
litebearer Posted December 27, 2010 Share Posted December 27, 2010 Although I prefer PFM's method, you can achieve your desired result by changing this... echo '<tr><td>' . $row['date_created'] . '</td></tr>'; to this... ?> <tr><td><?PHP echo date("F j, Y", $row['date_created']); ?></td></tr> <?PHP Quote Link to comment https://forums.phpfreaks.com/topic/222724-insert-into-db-datetime-and-echo-out-only-date/#findComment-1151781 Share on other sites More sharing options...
chaseman Posted January 21, 2011 Author Share Posted January 21, 2011 For every body who is wondering the same question I finally solved this problem with this solution: while ($row2 = mysqli_fetch_array($data2)) { echo '<table><tr><td>' . date('M d, Y', strtotime($row['created_date'])) . '</td></tr></table>'; } mysqli_close($dbc); The solution is date(' ', strtotime()), with the M d, Y you can display the time how ever you want it, BUT the date still gets as a full string inserted into the database with the posted time in the query with now(), as described in the first post. I'll mark this as solved. Quote Link to comment https://forums.phpfreaks.com/topic/222724-insert-into-db-datetime-and-echo-out-only-date/#findComment-1163361 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.