Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extracting content from meta tag PHP MYHTML DOM 3 22
Ajax and PHP 4 31
Use Select Query to Return Results as a Form 9 25
Length of for loop to be dynamic 2 14
These days socially coordinated efforts have turned into a critical requirement for enterprises.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
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.

820 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