How do I capture date and time as a variable and insert into mysql?
Posted on 2011-09-22
I need to capture the current date (YYYY-MM-DD) into trxdate variable, and the current time(HH:MM:SS) into trxtime variable. I then simply want to use these variables to insert into mysql db. Although there may be other ways to insert a date and time into mysql. I want to capture and use variables because I need these values further down in the php code.
I have tried many different formats and cannot get the date and time variables set up properly. Please advise.
Here is the php code that I am using:
$conn = mysql_connect("localhost", "X", "Y") or die(mysql_error());
$datevar = now();
$vardate =(date("YYYY-MM-DD", $datevar);
$timevar = now();
$vartime = date("HH-MM-SS", $timevar);
mysql_query("INSERT INTO itemhdr (ukey, user, trxdate, trxtime, email_add, email_sent)
VALUES ('1234567890', 'username', '$vardate', '$vartime', 'email@example.com', 'N')");