Solved

How do I capture date and time as a variable and insert into mysql?

Posted on 2011-09-22
3
322 Views
Last Modified: 2012-05-12
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);
?>
0
Comment
Question by:kbios
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 36582761
now(); is not a PHP function though NOW() is a MySQL function you can use in an SQL statment.  The code below should do what you want and get the current date and the current time.  trxdate and trxtime should be defined as DATE and TIME columns if you need to do any DATE/TIME functions with them.
$vardate = date("Y-m-d");
  $vartime = date("H:i:s");

Open in new window

0
 
LVL 3

Expert Comment

by:mykde
ID: 36582770
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
0
 

Author Closing Comment

by:kbios
ID: 36582809
Thanks. I had tried the date() earlier but used the format date("YYYY-MM-DD") and date("HH:MM:SS"). Thanks for the help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now