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 :)

Recommended Answers

All 8 Replies

instead of

SELECT * ...

use:

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

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.

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.

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

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.

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.