Stefany93 Posted June 21, 2012 Share Posted June 21, 2012 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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 21, 2012 Share Posted June 21, 2012 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 21, 2012 Share Posted June 21, 2012 Third your using the wrong datetime format in the database. It should be: YYYY-MM-DD HH:MM:SS Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2012 Share Posted June 21, 2012 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 . . . Quote Link to comment Share on other sites More sharing options...
Stefany93 Posted June 22, 2012 Author Share Posted June 22, 2012 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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 25, 2012 Share Posted June 25, 2012 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....){ Quote Link to comment Share on other sites More sharing options...
Stefany93 Posted June 25, 2012 Author Share Posted June 25, 2012 ^^ Thank you very much for the help. You are awesome! Quote Link to comment Share on other sites More sharing options...
debian23 Posted June 26, 2012 Share Posted June 26, 2012 if the record in database is ,,datetime,, then with this function date("Y-m-d H-i-s"); you will get the date and time in format yyyy-mm-dd hh-mm-ss. Hope that will help you ! Quote Link to comment Share on other sites More sharing options...
Barand Posted June 26, 2012 Share Posted June 26, 2012 @debian23 If it's datetime it's already stored as yyyy-mm-dd H:i:s Quote Link to comment Share on other sites More sharing options...
Stefany93 Posted June 26, 2012 Author Share Posted June 26, 2012 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 But you helped me a lot guys, thank you so much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.