Solved

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

Posted on 2011-09-22
3
327 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

738 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