kleb Posted December 19, 2011 Share Posted December 19, 2011 i have been trying to insert date with when users post there comment but when i echo the date() with the comments..it just display 0000.00.00.00 just like that and when i checked my DB it was like that too..please what can i do.this is my code Thankd in advance <?php include"header.php"; if(isset($_POST['submit'])) { $postdate=mktime(0,0,0,date("m"),date("d")+1,date("y")); $comment=mysql_real_escape_string($_POST['comment']); if($comment!=='') { $ins="INSERT INTO post(post_content,post_date)VALUES('$comment','$postdate')"; mysql_query($ins) or die(mysql_error()); } else { echo"You can not post an empty page"; } } Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 19, 2011 Share Posted December 19, 2011 Shoot me down if this is an overly simplistic suggestion...but could you not just use 'Time stamp' as one of your mysql table columns? This would then save you having to do all the date processing to create the date. i have been trying to insert date with when users post there comment but when i echo the date() with the comments..it just display 0000.00.00.00 just like that and when i checked my DB it was like that too..please what can i do.this is my code Thankd in advance <?php include"header.php"; if(isset($_POST['submit'])) { $postdate=mktime(0,0,0,date("m"),date("d")+1,date("y")); $comment=mysql_real_escape_string($_POST['comment']); if($comment!=='') { $ins="INSERT INTO post(post_content,post_date)VALUES('$comment','$postdate')"; mysql_query($ins) or die(mysql_error()); } else { echo"You can not post an empty page"; } } Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 why not just put: $postdate = date("m/d/y/ H:i"); Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 19, 2011 Share Posted December 19, 2011 or $today = date("F j, Y, g:i a"); echo $today; But personally i would leave it to the database to do it. If you can pass something to database to process you probably should. Just my opinion. Quote Link to comment Share on other sites More sharing options...
kleb Posted December 19, 2011 Author Share Posted December 19, 2011 i have tried most of the options here but with no thesame result... Quote Link to comment Share on other sites More sharing options...
kleb Posted December 19, 2011 Author Share Posted December 19, 2011 how do i use the time stamp..do i write it together as in TIMESTAMP? Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 19, 2011 Share Posted December 19, 2011 You can do a time stamp just by going into your database on php myadmin. Then add a column to the table and in the drop down for 'type' (that's the dropdown with VARCHAR, INT etc.) there is an option for DATE and TIME. You can select various variations on the date and time from the drop down. Then everytime a record is added to the database it will automatically add the current date and time to that field. So you don't have to do it. You can then retreive the date and time as you would any other database value. how do i use the time stamp..do i write it together as in TIMESTAMP? Quote Link to comment Share on other sites More sharing options...
kleb Posted December 19, 2011 Author Share Posted December 19, 2011 still showing this 0000-00-00 00:00:00 even when i changed it to TIMESTAMP Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted December 19, 2011 Share Posted December 19, 2011 Hmm ok. Go into myadmin and click on structure then click to modify the column for time stamp. In there there's a drop down for DEFAULT with a dropdown. Select CURRENT_TIMESTAMP from the drop down. Hopefully that'll fix it Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2011 Share Posted December 19, 2011 In order for the timestamp to update, an UPDATE operation has to be run on the record. Simply changing the data type won't update all the timestamps. Quote Link to comment Share on other sites More sharing options...
kleb Posted December 19, 2011 Author Share Posted December 19, 2011 Hi drongo i truely appreciate your effort..but it still thesame thing infact everything is set its even on UPDATE when i checked it..please what do i do? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2011 Share Posted December 19, 2011 In order for the timestamp to update, an UPDATE operation has to be run on the record. Simply changing the data type won't update all the timestamps. Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 If the date and time is being sent to the database (they are not displaying properly) then there is no reason why this should not work: <?php include"header.php"; if(isset($_POST['submit'])) { $postdate = date("m/d/y/ H:i"); $comment=mysql_real_escape_string($_POST['comment']); if($comment!=='') { $ins="INSERT INTO post(post_content,post_date)VALUES('$comment','$postdate')"; mysql_query($ins) or die(mysql_error()); } else { echo"You can not post an empty page"; } } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2011 Share Posted December 19, 2011 There is a reason that won't work. The format of a TIMESTAMP field is not M/D/Y h:m, it's YYYY-MM-DD HH:MM:SS. Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 There is a reason that won't work. The format of a TIMESTAMP field is not M/D/Y h:m, it's YYYY-MM-DD HH:MM:SS. Wouldn't it just be easier to create a new column in the table called 'date' or something and use date() to set it? I use it and it works without fault. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2011 Share Posted December 19, 2011 Dates should be stored in the database's native format, which for MySQL is YYYY-MM-DD HH:MM:SS. Doing so allows you to make use of the many date/time functions built in to MySQL. Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 20, 2011 Share Posted December 20, 2011 So how would I go about setting it in my script? Or is there a tool in phpMyAdmin? Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 20, 2011 Share Posted December 20, 2011 Never mind, I have done it, I think. Would this do? $dateTime = new DateTime("now", new DateTimeZone('GMT')); $date = $dateTime->format("Y-m-d H:i:s"); Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 20, 2011 Share Posted December 20, 2011 Do you mean to insert it into the database? INSERT INTO table ( datetime_field ) VALUES ( NOW() ) Quote Link to comment Share on other sites More sharing options...
kleb Posted December 20, 2011 Author Share Posted December 20, 2011 i hv done that already and its not working Quote Link to comment Share on other sites More sharing options...
kleb Posted December 20, 2011 Author Share Posted December 20, 2011 hi Mellor God bless you a million times..it worked..wow am so happy now it shows the date instead of the 000.00.00.00 it showed before..but the date is not correct Quote Link to comment Share on other sites More sharing options...
kleb Posted December 20, 2011 Author Share Posted December 20, 2011 :D :D :D :D :D i thank you all for all your concern May the good lord richly bless you all. thanks Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 20, 2011 Share Posted December 20, 2011 hi Mellor God bless you a million times..it worked..wow am so happy now it shows the date instead of the 000.00.00.00 it showed before..but the date is not correct No problem. Is it because I set it to GMT? It is correct for me, but you could change it to what is right for you Quote Link to comment Share on other sites More sharing options...
kleb Posted December 20, 2011 Author Share Posted December 20, 2011 pls how will i change it to west african time Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 20, 2011 Share Posted December 20, 2011 pls how will i change it to west african time ('GMT+1') 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.