Solved

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

Posted on 2011-09-22
3
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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