0

Hi..
I got a problem with the date and time in php..
This is the story.

1)I want to get the date and time automatically whenever a person write onto the field that i have made(eg. the blogpost field).

2)Then,after i stored it into the database, i want to output it into my desired format.That is like this :

September 8 2010 at 11:46am


And what i have done was created a field of dateandtime and made its stored data as the timestamp type in my phpmyadmin.

BELOW IS THE WORKS :

This is the part where someone entering something into the field :

..some codes here...
<textarea rows="18" id="postcontent" name="blogpost" cols="100"></textarea>
...some codes here...

and after clicked submit,this page will be redirected to the other page:

This is the page and my query to insert the date and time :

<?php
$blogpost=$_POST['blogpost'];

$query = "INSERT INTO blogpost,dateandtime (blogpost,dateandtime) VALUES ( '{blogpost}',NOW() ) ";
?>

This is how I echoing the date on the mainpage :

$result = mysql_query("SELECT * FROM dateandtime");

while($row=mysql_fetch_array($result))
{
echo "$row["blogpost"]".$row["dateandtime"];
}

Thus,by using the timestamp,how could i do that format that i want?Or there is the other way that i could use to get the date automatically and format it into that way?

Please help.Thank You :)

Edited by samsons17: n/a

3
Contributors
8
Replies
10
Views
6 Years
Discussion Span
Last Post by samsons17
0

instead of

SELECT * ...

use:

SELECT blogpost, DATE_FORMAT(dateandtime,'%M %e %Y at %h:%i%p') as dateandtime ...
0

http://lmgtfy.com/?q=timestamp+php

dont you read my post first?i am using now() function to get the current date and time from the user meaning it'll get them automatically..

the question again,is how do i format my date and time so that it will appear as this:

September 8 2010 at 11:46am

i already look around the php.net again and again,but i am not sure how could i do this,thats why i am asking here.So instead of giving the link,could you show me some example please..

tq.

0

try my post.

SELECT blogpost, DATE_FORMAT(dateandtime,'%M %e %Y at %h:%i%p') as dateandtime FROM TABLENAME

just replace TABLENAME with the actual name of your table.

Edited by hielo: n/a

0

try my post.

SELECT blogpost, DATE_FORMAT(dateandtime,'%M %e %Y at %h:%i%p') as dateandtime FROM TABLENAME

just replace TABLENAME with the actual name of your table.

Sory for a late reply..
I have done like what you said,but i dont know why,i am getting an error...

here is my actual code :

<?php
$blogpost=("SELECT id,title,blogpost, DATE_FORMAT(dp_datetime,'%M %e %Y at %h:%i%p') as dp_datetime FROM blogpost");
 
		while($row = mysql_fetch_array($blogpost))
			{
		$id = $row["id"];
		$result2 = mysql_query ("SELECT id FROM comments WHERE entry='$id'");
        $total_comments = mysql_num_rows($result2);

				echo "<div id='blogcontainer'>"."<div id='blogtitle'>".$row["title"]."</div>".
			"<br/>"."<div id='blogpost'>"."<div id='mainpost'>".$row["blogpost"]."</div>"."<br />"."<div id='date'>".$row["dp_datetime"]."</div>"."<br/>".
			"<a href='#lightbox' class='commenttoggle' rel='facebox'>".$total_comments." "."comment</a>".
			
				"</div>";	
	}
				
		?>
0

on line 2 you forgot to call mysql_query() so that it ACTUALLY executes the query. You are simply assigning a string (the sql statement) to $blogpost.

0

on line 2 you forgot to call mysql_query() so that it ACTUALLY executes the query. You are simply assigning a string (the sql statement) to $blogpost.

WOW!...you are great...
its actually works like magic!

Thank you so much for helping :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.