Link to home
Start Free TrialLog in
Avatar of kbios
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);
?>
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think this is what your after:

<?php

$conn = mysql_connect("localhost", "X", "Y") or die(mysql_error());
mysql_select_db("test");


$vardate = date("Y-m-d"); // Example: 2011-09-22

$vartime = date("H:i:s"); // Example: 12:03:15

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);

?>

Open in new window

http://us2.php.net/manual/en/function.date.php
Avatar of kbios
kbios

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.