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"; } } Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/ 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"; } } Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299312 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"); Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299319 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. Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299320 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... Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299322 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? Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299323 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? Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299338 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 Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299360 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 Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299386 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. Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299390 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? Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299397 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. Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299399 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299459 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. Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299515 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. Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299517 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. Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299519 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? Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299554 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"); Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299557 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() ) Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299572 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 Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299633 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 Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299640 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 Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299641 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 Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299754 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 Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299761 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') Link to comment https://forums.phpfreaks.com/topic/253474-date-function-doesnt-insert-into-the-database/#findComment-1299765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.