Dislaying Date from MySQL Database

Post here if you need help with SQL.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Dislaying Date from MySQL Database

Post 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.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Dislaying Date from MySQL Database

Post by nyo »

Ok, found the answer.

I use
date('d.m.Y H:i', $comment['time'])
instead of $posttime to echo the date.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Dislaying Date from MySQL Database

Post by jacek »

self solving topics :D
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Dislaying Date from MySQL Database

Post by nyo »

jacek wrote:self solving topics :D
Yeah :) might help others too.
Post Reply