Jump to content

How to extract and display the datetime from the MySQL DB corectly?


Stefany93

Recommended Posts

Hello colleagues,

 

 

I am trying to create blog and so far it has been going great, however I have came across a very annoying problem. The thing is that I want to extract the datetime from my blog and to be shown under each post. The title and the content has been displaying OK in the blog page, but the problem is that every time I loop the datetime that has been stored in the DB it is display four times, and I don't want this to happen, plus I seem can't set it each row of datetime to be displayed only on the post with the same ID in the DB.

 

Please, please tell me what to do!

 

This is how the data is aligned at the DB:

 

id title content datetime

 

The format I use to save the current date and time in DB is DATETIME with a default value of 00-00-0000 00:00:00

and then in the mysql query I use the NOW() function to update the values.

 

Here is the code:

 

	
			$query = mysql_query("SELECT id, title,date_posted, contents FROM posts");

				if(mysql_num_rows($query)){


				while($result = mysql_fetch_assoc($query)){


					 $title[] = $result['title'];
					 $content[] = $result['contents'];
					 $time[] = $result['date_posted'];
				}



			}

			function post(){


			global $title;
			global $content;
			global $time;
				foreach(array_combine($title,$content) as $zaglavie=>$statia){

					echo '<h1>'.$zaglavie.'</h1><p>'.$statia.'</p>';


				}

			}

 

 

I am using the post() funtion inside the index page of the blog where the values from the DB are supposed to be displayed.

 

Thank you very much!

 

Link to comment
Share on other sites

First, don't use globals, pass the variables as arguments.

Secondly, you should be able to just echo the $time. If you want to format it, you can use strtotime and then date. Or use the php DateTime library.

Link to comment
Share on other sites

Third your using the wrong datetime format in the database.

 

It should be:

 

YYYY-MM-DD HH:MM:SS

 

The OP did say the field type was DATETIME, and the date is displaying just fine, so that would imply it was just a typo when showing us the default value . . .

Link to comment
Share on other sites

Thank you very much for the help, fellows! You helped me very much! One last question please. You said that it is wrong to use globals, could you tell me how to make the variables global without using the keyword, please?

 

Thank you!

 

 

P.S and yes, there was a typo while I was writing down the date. Indeed it is 0000-00-00 00:00:00. Sorry!

Link to comment
Share on other sites

Don't make the variables global. It's not matter of using the keyword or not, it's a matter of not doing it.

 

Your function declaration should look like:

function post($title, $content, $time, etc....){

Link to comment
Share on other sites

Yeah indeed it was in this format, it is just made a typo in the previous post.

 

I already displayed the time and date using the while loop. Turned out I could this is all along. I felt to stupid :D

 

But you helped me a lot guys, thank you so much!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.