Reputation: 16897
I'm trying to insert the current date from a php form into a database.
My query is:
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', 'date()')";
This produces the following: 0000-00-00 00:00:00 which obviously isn't right.
Can anyone recommend how to get this to display the correct date/time?
Also, I need it in the following format: yyyymmddhhmmss - Is this possible? How would I go about it?
Thanks!
Upvotes: 2
Views: 962
Reputation: 5340
I presume your date
column is actually of mysql DATETIME type.
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', NOW())";
should do it.
You can store it in DATETIME type (you'll see it as yyyy-mm-dd hh:mm:ss)and then format it to yyyymmddhhmmss
when you SELECT it out of the database:
SELECT DATE_FORMAT(date, '%Y%m%d%H%i%s') FROM orderDetails
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Upvotes: 0
Reputation:
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', 'date(dmy)')";
or
$time = date('Ymd g:i');
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', " .$time . ")";
Upvotes: 0
Reputation: 57278
You should use php's function time()
or mysql's UNIX_TIMESTAMP
this is an int(11)
and used like so:
INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', UNIX_TIMESTAMP --
Then you create a visual timestamp from php using the date()
function.. during your loop in PHP.
Upvotes: 1
Reputation: 596
Refer to the PHP Manual regarding the use of date(): http://php.net/manual/en/function.date.php
So for your needs it would be something like:
$date = date("YmdHis");
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', '$date')";
Also you might take into account the advice from Fabrik. Using UNIX timestamp is probably the most reliable way to go.
Upvotes: 0
Reputation:
$date = date ( 'Y-m-d H:i:s');
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', '$date')";
Upvotes: 0
Reputation: 10477
Use SQL NOW()
function:
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', NOW()";
Upvotes: 1