niven300
niven300

Reputation: 59

date() function in PHP

Whats the correct format using the date() function in PHP to insert into a MySQL date type column? or am I using the wrong function? I need to be able to insert the current date into the database via a mysql_query.

EXAMPLE

$insert = mysql_query("INSERT INTO booking (test1,test2,test3,Date_Booked) VALUES ('". $var1. ",". $var2.",". $var3 .","date()"')");

Upvotes: 1

Views: 129

Answers (3)

kashan
kashan

Reputation: 101

$insert = mysql_query("INSERT INTO

booking (test1,test2,test3,Date_Booked)

VALUES

('$var1','$var2','$var3','".date('Y-m-d H:i:s')."')");

Upvotes: 0

David Wilkins
David Wilkins

Reputation: 584

My code, I use date("Y-m-d H:i:s") for a field of type "DATETIME" but this will depend on the data structure of your table. There are other date string that may work depending. Can you provide the structure of the date field in your table so I can revise my answer if needed?

Upvotes: 1

John Conde
John Conde

Reputation: 219794

Assuming your Date_Booked column is expecting a date value it would be date('Y-m-d'). A better alternative might be to use MySQL's own date functionality. Using CURDATE() or NOW() would also accomplish the same thing:

$insert = mysql_query("INSERT INTO booking (test1,test2,test3,Date_Booked) 
    VALUES ('". $var1. ",". $var2.",". $var3 .",NOW())");

$insert = mysql_query("INSERT INTO booking (test1,test2,test3,Date_Booked) 
    VALUES ('". $var1. ",". $var2.",". $var3 .",CURDATE())");

Upvotes: 3

Related Questions