Page 1 of 1

Dislaying Date from MySQL Database

Posted: Sat May 14, 2011 7:22 am
by nyo
Hi,

I am working on a guestbook and I need to display the date of each post which is stored as int in the database. The name of the column is posttime and they are stored like 1211375284.

Here is my current code:
<?php include 'includes/header.php'; ?>
<?php
	//function to format date
	function formatdate($timeformat,$timestamp) {
 	global $timezoneoffset;

 	$summertime = date("I")*3600;
 	$timestamp+=3600*intval($timezoneoffset)+$summertime;
 	return gmdate($timeformat, $timestamp);
	}
	//connect database
	$db_host = "...";
	$db_username = "...";
	$db_pass = "...";
	$db_name = "...";
	$db = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");
	mysqli_set_charset($db, 'utf8');
	//fetch data from database
	$result = mysqli_query($db, 'SELECT authorname, message, posttime, visible FROM bgb2_posts WHERE visible = 1 ORDER BY posttime DESC');
	while ($row = mysqli_fetch_array($result))
	{
		$comments[] = array('author' => $row['authorname'], 'message' => $row['message'], 'time' => $row['posttime']);
	}
	
	$posttime = formatdate($timeformat, $comments['time']);
	
?>
	
<div id="title"><a href="tr.php">Ana Sayfa</a> » Ziyaretçi Defteri</div>
<div id="content">
//display author, comment and time
<?php foreach ($comments as $comment):
	echo "<div id ='comments'><p class='textbold'>" . $comment['author'] . " - " . $posttime . "</p>";
	echo "<p>" . $comment['message'] . "</p></div>";
endforeach;
?>

</div>
<?php include 'includes/footer.php'; ?>
I found a function, formatdate, from somewhere and trying to implement it to my code but I don't know how to insert it into foreach loop properly.

Re: Dislaying Date from MySQL Database

Posted: Sat May 14, 2011 8:27 am
by nyo
Ok, found the answer.

I use
date('d.m.Y H:i', $comment['time'])
instead of $posttime to echo the date.

Re: Dislaying Date from MySQL Database

Posted: Sat May 14, 2011 12:15 pm
by jacek
self solving topics :D

Re: Dislaying Date from MySQL Database

Posted: Sat May 14, 2011 2:27 pm
by nyo
jacek wrote:self solving topics :D
Yeah :) might help others too.