Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » I need some PHP help! timestamp?
Closed Thread
Old 04-30-2009, 05:39 PM   #1 (permalink)
 
Newb Techie

Join Date: Mar 2009

Location: San Marcos CA

Posts: 15

Hereisfun is on a distinguished road

Send a message via AIM to Hereisfun
Default I need some PHP help! timestamp?

Hello My comment system has been up for a while but i was wondering how to make it so it would say the date/time when it was posted.
Right now it looks like this:

Name: My name
Comment: My comment

I would like it to look like this:

Name: My name
Comment: My comment
Date posted

Do i need to make a new row in my db?
Thanks
Hereisfun is offline  
Old 05-01-2009, 12:57 AM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: I need some PHP help! timestamp?

You need to make a new column in your database table as int(11).

Then, when your script inserts the comment, use the PHP time() function as the data for that column.

Then, when you display your column, use the PHP date() function to format the timestamp.

Example:

Insert
PHP Code:
<?php

$name 
$_POST['name'];
$comment $_POST['comment'];

$sql mysql_query ("INSERT INTO comments (id,name,comment,date) VALUES (0,'".$name."','".$comment."','".time()."')");

?>
Display
PHP Code:
<?php

$sql 
mysql_query ("SELECT * FROM comments");

while (
$row mysql_fetch_array ($sql)) {
     echo 
'Name: '.$row['name'].'<br />Comment: '.$row['comment'].'<br />Date: '.date('M/d/Y h:i A'$row['date']);
}

?>
The above would display something like:

Name: My name
Comment: My comment
Date: 05/01/2009 01:30 AM

You can change the way it is formatted by changing the date() parameters. See a list of parameters here.

Hope that helps.
__________________

Need website help? PM me!
CrazeD is offline  
Old 05-01-2009, 02:17 PM   #3 (permalink)
 
Newb Techie

Join Date: Mar 2009

Location: San Marcos CA

Posts: 15

Hereisfun is on a distinguished road

Send a message via AIM to Hereisfun
Default Re: I need some PHP help! timestamp?

it helped but the date is off like alot

Name: Name
Comment: Comment
Date: Dec/31/1969 05:00 PM

Looked like that :x
Hereisfun is offline  
Old 05-01-2009, 03:17 PM   #4 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: I need some PHP help! timestamp?

Show me your code where you display that please.
__________________

Need website help? PM me!
CrazeD is offline  
Old 05-02-2009, 02:22 AM   #5 (permalink)
 
Newb Techie

Join Date: Mar 2009

Location: San Marcos CA

Posts: 15

Hereisfun is on a distinguished road

Send a message via AIM to Hereisfun
Default Re: I need some PHP help! timestamp?

Here is my form and displaying comment:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Leave a comment.</title>
</head>

<body>


<font color="#000000"><form method="post" action="thankyou.php">

<label for="name"><b>First Name:</b></label><br />

<input type="text" id="name" name="name" /><br />
<br />
<label for ="comment"><b>Comment:</b></label><br />

<input name="comment" type="text" id="comment" /><br />
<br />
<label for="email"><b>Email: </b></label><br />

<input name="email" type="text" id="email" /><br />

<input type="submit" value="Enter Comment" name="submit" />

</form></font><br /><br />

<font color="#000000"><?php

$dbc = mysqli_connect('host', 'Hereisfun', 'password', 'Hereisfun')
or die('Error connectiong to MYSQL server.');

$sql = mysqli_query($dbc, "SELECT * FROM cmmnts2");

while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
echo 'Name: '.$row['name'].'<br />Comment: '.$row['comment'].'<br />Date: '.date('M/d/Y h:i A', $row['date']);
}

?></font>

</body>
</html>




And here is my inserting into db code:


<html>
<head>
<title> Thank you for entering your comment. </title>

</head>
<body link="#000000" bgcolor="#CCCCCC">

<font color="#000000"><div align="center"><?php

$name = $_POST['name'];
$comment = $_POST['comment'];
$email = $_POST['email'];

$dbc = mysqli_connect('host', 'Hereisfun', 'password', 'Hereisfun')
or die('Error connectiong to MYSQL server.');

$query = "INSERT INTO cmmnts2 (name, date, email, comment) " .
"VALUES ('$name', ' .time(). ', '$email', '$comment')";



$result = mysqli_query($dbc, $query) or die('Error querying database.');
mysqli_close($dbc);
echo 'Thanks for submitting your comment!<br />';
echo 'Name: ' . $firstname . '<br />';
echo 'Comment: ' . $comment . '<br />';
echo 'Email: ' . $email . '<br />';
?>

<p><a href="comment.php"> Post another comment.</a></p>
</div>
</font>

</body>


---------
Its pretty much a blank page at the moment, the comments work fine i just need to add some line breaks...the time thing is always 1969 ^-^;
Hereisfun is offline  
Old 05-02-2009, 11:17 PM   #6 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: I need some PHP help! timestamp?

Did you add the "date" column to your table?
__________________

Need website help? PM me!
CrazeD is offline  
Old 05-03-2009, 02:45 AM   #7 (permalink)
 
Newb Techie

Join Date: Mar 2009

Location: San Marcos CA

Posts: 15

Hereisfun is on a distinguished road

Send a message via AIM to Hereisfun
Default Re: I need some PHP help! timestamp?

Yeppers
Hereisfun is offline  
Old 05-03-2009, 11:32 AM   #8 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: I need some PHP help! timestamp?

On your display page, please do this:

PHP Code:
while ($row mysqli_fetch_array($sqlMYSQLI_ASSOC)) {
echo 
'Name: '.$row['name'].'<br />Comment: '.$row['comment'].'<br />Date: '.date('M/d/Y h:i A'$row['date']);

$data[] = $row;
}
echo 
'-------<br />';
print_r($data);
echo 
'<br />-------'
And then paste back what it displays.
__________________

Need website help? PM me!
CrazeD is offline  
Old 05-03-2009, 07:02 PM   #9 (permalink)
 
Newb Techie

Join Date: Mar 2009

Location: San Marcos CA

Posts: 15

Hereisfun is on a distinguished road

Send a message via AIM to Hereisfun
Default Re: I need some PHP help! timestamp?

This is what showed:
Name: My name
Comment: My comment
Date: Dec/31/1969 05:00 PM-------
Array ( [0] => Array ( [name] => My name [comment] => My comment [email] => My email [date] => 0 ) )
-------
Hereisfun is offline  
Old 05-05-2009, 03:50 PM   #10 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: I need some PHP help! timestamp?

In your insert script, you have dots before and after the time().

PHP Code:
'$name'' .time(). ''$email''$comment' 
Should be like this:

PHP Code:
'$name''time()''$email''$comment' 

__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Guide to Installing Apache, PHP, MySQL, and PHPMyAdmin on Windows CrazeD Programming Discussions 17 06-17-2009 04:27 PM
How echo works in PHP Osiris Programming Discussions 0 03-04-2009 08:34 AM
PHP - what it does and what it doesn’t Osiris Programming Discussions 1 02-16-2009 04:09 PM
Wev Development: How does PHP work? Osiris Programming Discussions 1 01-08-2009 04:31 PM
Removing PHP For Increased Download Speed Rex100 Programming Discussions 4 07-21-2008 03:49 PM