kbios
asked on
How do I capture date and time as a variable and insert into mysql?
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:
<?php
$conn = mysql_connect("localhost", "X", "Y") or die(mysql_error());
mysql_select_db("test");
$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', 'info@xyz.com', 'N')");
mysql_close($conn);
?>
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:
<?php
$conn = mysql_connect("localhost",
mysql_select_db("test");
$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', 'info@xyz.com', 'N')");
mysql_close($conn);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I had tried the date() earlier but used the format date("YYYY-MM-DD") and date("HH:MM:SS"). Thanks for the help.
Open in new window
http://us2.php.net/manual/