Davie33 Posted January 11, 2012 Share Posted January 11, 2012 Hi am trying to add date and timestamp of when a comment gets posted.Am not gr8t at php but still learning. If any one can help me on this would be reil gr8t. Sry for the long post. As for now i have this $date = date("M j, y, g:i a"); and echo's out the date, time [/code]$date[/code] as in the current date and time which i dont want. <?php $query = yasDB_select("SELECT * FROM newsblog"); if($query->num_rows == 0) { echo '<div id="newsblog_text">This news blog has no comments, become a member and be the first to add one!</div>'; } else { $query = yasDB_select("SELECT * FROM newsblog WHERE userid = '$id'"); while($row = $query->fetch_array(MYSQLI_ASSOC)) { $date = date("M j, y, g:i a"); $text = $row['comment']; $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/biggrin.gif" title="biggrin" alt="biggrin" />',$text); $text = str_replace(':?','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/confused.gif" title="confused" alt="confused" />',$text); $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/cool.gif" title="cool" alt="cool" />',$text); $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/cry.gif" title="cry" alt="cry" />',$text); $text = str_replace(':shock:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/eek.gif" title="eek" alt="eek" />',$text); $text = str_replace(':evil:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/evil.gif" title="evil" alt="evil" />',$text); $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/lol.gif" title="lol" alt="lol" />',$text); $text = str_replace(':x','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/mad.gif" title="mad" alt="mad" />',$text); $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/razz.gif" title="razz" alt="razz" />',$text); $text = str_replace(':oops:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/redface.gif" title="redface" alt="redface" />',$text); $text = str_replace(':roll:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/rolleyes.gif" title="rolleyes" alt="rolleyes" />',$text); $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/sad.gif" title="sad" alt="sad" />',$text); $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/smile.gif" title="smile" alt="smile" />',$text); $text = str_replace('','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/surprised.gif" title="surprised" alt="surprised" />',$text); $text = str_replace(':twisted:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/twisted.gif" title="twisted" alt="twisted" />',$text); $text = str_replace(':wink:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/wink.gif" title="wink" alt="wink" />',$text); echo '<div class="newsblog_box1"><div class="newsblog_name">' . $row['name'] . ' '.$date.'</div></div> <div class="newsblog_box2">' . $text . '</div>'; } }?> Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/ Share on other sites More sharing options...
MasterACE14 Posted January 11, 2012 Share Posted January 11, 2012 date("M j, y, g:i a", $row['timestamp']); Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306689 Share on other sites More sharing options...
Davie33 Posted January 11, 2012 Author Share Posted January 11, 2012 date("M j, y, g:i a", $row['timestamp']); Thanks for the fast reply.Do i need to add timestamp to database and how would i do this if so ?. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306694 Share on other sites More sharing options...
jcbones Posted January 12, 2012 Share Posted January 12, 2012 Make a new column, set it to default at the current timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306748 Share on other sites More sharing options...
Davie33 Posted January 12, 2012 Author Share Posted January 12, 2012 Make a new column, set it to default at the current timestamp. Thanks for the reply. So to add to database it should be like this ? `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, Ps: thanks bud for the help got it working thanks . I will leave this post for others to veiw so the know what to do if they ever need something like this thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306753 Share on other sites More sharing options...
Davie33 Posted January 12, 2012 Author Share Posted January 12, 2012 Hi again for some reason the am or pm is not showing up i have tryed with add , to date("M j, y, g:i, a", $row['timestamp']); but no go. Can you help me one last time plz thanks. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306762 Share on other sites More sharing options...
jcbones Posted January 12, 2012 Share Posted January 12, 2012 Have you tried putting an A in there, notice the capital. See if that works. I've never had a problem getting am/pm to show. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306763 Share on other sites More sharing options...
Davie33 Posted January 12, 2012 Author Share Posted January 12, 2012 Yeah bud tryed a and A not working. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306765 Share on other sites More sharing options...
Davie33 Posted January 12, 2012 Author Share Posted January 12, 2012 I have found out the problem why its not working with am & pm as this date("M j, y, g:i a", $row['timestamp']); doesn't mean anything to my code and the database but one thing i did leave in was $row['timestamp']) and the sql i added to show date and time but its showing date and time like this 2012-01-12 03:39:15 . like this... echo '<div class="newsblog_box1"><div class="newsblog_name">' . $row['name'] . ' '.$row['timestamp'].'</div></div> <div class="newsblog_box2">' . $text . '</div>'; This is all done on my localhost server. Date and time show no problem apart from am and pm. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306775 Share on other sites More sharing options...
jcbones Posted January 12, 2012 Share Posted January 12, 2012 To get the desired format using MySQL's date functions, add this to your select statement. DATE_FORMAT(`timestamp`,'%b %e, %y, %r') AS formatted_time as in: SELECT *,DATE_FORMAT(`timestamp`,'%b %e, %y, %r') AS formatted_time FROM table; Then change your output to: echo '<div class="newsblog_box1"><div class="newsblog_name">' . $row['name'] . ' '.$row['formatted_time'].'</div></div> <div class="newsblog_box2">' . $text . '</div>'; Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306782 Share on other sites More sharing options...
Davie33 Posted January 12, 2012 Author Share Posted January 12, 2012 Thanks bud i will get back to you later on this. Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306788 Share on other sites More sharing options...
Davie33 Posted January 13, 2012 Author Share Posted January 13, 2012 Hi i tryed what you said but comes up error. This is the query $query = yasDB_select("SELECT * FROM newsblog WHERE userid = '$id' IN(SELECT *,DATE_FORMAT(`timestamp`,'%b %e, %y, %r') AS formatted_time FROM newsblog)"); This is the error. Error thrown by database select "SELECT * FROM newsblog WHERE userid = '2' IN(SELECT *,DATE_FORMAT(`timestamp`,'%b %e, %y, %r') AS formatted_time FROM newsblog)" Operand should contain 1 column(s) Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1307142 Share on other sites More sharing options...
jcbones Posted January 13, 2012 Share Posted January 13, 2012 No, you must have mis-understood what I said. $query = yasDB_select("SELECT *,DATE_FORMAT(`timestamp`,'%b %e, %y, %r') AS formatted_time FROM newsblog WHERE userid = '$id'"); Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1307382 Share on other sites More sharing options...
Davie33 Posted January 13, 2012 Author Share Posted January 13, 2012 No, you must have mis-understood what I said. $query = yasDB_select("SELECT *,DATE_FORMAT(`timestamp`,'%b %e, %y, %r') AS formatted_time FROM newsblog WHERE userid = '$id'"); I think i did mis-understood you sry bud and thanks this works gr8t just what i was looking for.Thanks again . Quote Link to comment https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1307383 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.