Page 1 of 1
Insert current date automatically
Posted: Mon Nov 03, 2003 12:30 am
by 3.14
I have a date script:
Code: Select all
<?php
echo date ("l dS of F Y");
?>
Which generates today's date. My MySQL database has a field named date. Essentially I want to insert this date into the database with the click of a button.
EXAMPLE:
Person loads form. Person types in message. Person presses submit. Person is taken back to main page where message is displayed as is the date and time.
So, can this be done? If so... how?

Posted: Mon Nov 03, 2003 4:02 am
by twigletmac
You need to format the date for MySQL, if you are storing the date in a DATE field, then it would be like so (yyyy-mm-dd):
Mac
Posted: Mon Nov 03, 2003 4:10 am
by Linkjames
You have two options here. Either create a variable from the PHP date function and insert that into the database, or instruct MySql to store the date for you.
To store the PHP date function simply create a variable from date
Code: Select all
<?php
$date = date("l dS of F Y");
?>
then just insert $date into the date field in your database along with the message. However, you are then stuck with this format. You cannot then change the layout of dates already stored.
The alternative is to simply create a date field in MySql and use one of the date storage options for the field type. Personally I like to use timestamp. Then whenever an entry is made in your database, it will automatically record the time (Unless you tell it not to). However you will need to format the date in your query when you call the information from the database.the advantage of this is that you can change the format in your query, so you can change how the date displays at will.
Hope that helps.
Posted: Mon Nov 03, 2003 4:13 am
by Linkjames
I knew someone was going to reply to that before I did

Posted: Mon Nov 03, 2003 4:18 am
by qads
the query will look something like
Code: Select all
$query = "INSERT INTO `table` (`date`) VALUES ('$date')";
Posted: Mon Nov 03, 2003 4:46 am
by itsmani1
hi there.....
wel u hav to wait for some time because i hav done all this stuff but it's not with me right now.
fortunately i did it last night and i can giv it to u in 2hours
means @ 5:45 PST(Pakistan Standard Time)
Regardssssssssss
Posted: Mon Nov 03, 2003 5:10 am
by twigletmac
itsmani1 wrote:hi there.....
wel u hav to wait for some time because i hav done all this stuff but it's not with me right now.
fortunately i did it last night and i can giv it to u in 2hours
means @ 5:45 PST(Pakistan Standard Time)
Regardssssssssss
Why not just hold off posting until you do have the code?
Mac
Posted: Mon Nov 03, 2003 7:54 pm
by 3.14
Ok, I've done the following:
Code: Select all
<?php
$date = date ("j m Y h:i:s A");
// snipped out some text in between
$sql = mysql_query("INSERT INTO table1 (heading, current_date, body_text, message_id)
VALUES('$heading', '$date', '$body_text', '$message_id')") or die (mysql_error());
?>
And it brings me with an error:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'current_date, body_text, message_id) VALUES('This is a message heading', '4 11 20
What is going wrong? Logically, that should work.
Posted: Mon Nov 03, 2003 8:11 pm
by 3.14
Nevermind, there was an error with the tables.
Got it working

Posted: Mon Nov 03, 2003 11:31 pm
by itsmani1
thanks twigletmac.......
for ur commentsssss