views:

130

answers:

6
+2  Q: 

php date format

Hi guys,

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!

+1  A: 

Use SQL NOW() function:

$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', NOW()";
Tomasz Kowalczyk
Note: This works only when table scheme configured properly.
fabrik
Of course - if field is not DATETIME it's better to use the format from other answers.
Tomasz Kowalczyk
A: 
$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 . ")";
Jordy
A: 
$date = date ( 'Y-m-d H:i:s'); 
$sql3="INSERT INTO orderDetails (compName, package, cost, date) VALUES ('$companyName', '$package', '$cost_record', '$date')";
Denis
Note OP's question: "I need it in the following format: yyyymmddhhmmss"
fabrik
when selecting it back from DB you can convert it to any format you like
Denis
Hi Denis, should I set the field in my database to something other than TIMESTAMP for this to work? Thanks!
TaraWalsh
I'm using DATETIME type in MySql, but it should work correctly with TIMESTAMP too. Maybe you dont even need to generate date in PHP, you can use CURRENT_TIMESTAMP with TIMESTAMP read this for more info: http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
Denis
A: 

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.

brozo
A: 

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

Fanis
@fabrik May I ask why you added the `` in my second sql string when it was already tabbed in? I'm still new to this so am I missing some formatting convention?
Fanis
While it's surrounded by `s it wasn't parsed as code snippet. Now edited again it's fine.
fabrik
+1  A: 

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.

RobertPitt