Insert current date automatically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Insert current date automatically

Post 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? :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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):

Code: Select all

mysql_date = date('Y-m-d');
Mac
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post 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.
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

I knew someone was going to reply to that before I did ;)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

the query will look something like

Code: Select all

$query = "INSERT INTO `table` (`date`) VALUES ('$date')";
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post 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.
3.14
Forum Commoner
Posts: 28
Joined: Mon Sep 08, 2003 12:17 am

Post by 3.14 »

Nevermind, there was an error with the tables.

Got it working :)
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

thanks twigletmac.......


for ur commentsssss
Post Reply