Jump to content

Recommended Posts

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>';
	}
}?>  

Link to comment
https://forums.phpfreaks.com/topic/254831-date-and-timestamp/
Share on other sites

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 :D.

I will leave this post for others to veiw so the know what to do if they ever need something like this thanks again.

Link to comment
https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306753
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306775
Share on other sites

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>';  

Link to comment
https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1306782
Share on other sites

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)  

Link to comment
https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1307142
Share on other sites

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 :D.

Link to comment
https://forums.phpfreaks.com/topic/254831-date-and-timestamp/#findComment-1307383
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.