Hashim Al-Arab
Hashim Al-Arab

Reputation: 164

save datetime in mysql using php

I have table in mysql database which have field with datatype is datetime.

I want to save the datetime in this minute, for this I use " Now() ", but it does not work,

It just save 000000000000 in databaes.

Upvotes: 1

Views: 13449

Answers (7)

Abdullah
Abdullah

Reputation: 538

try this: set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:

INSERT INTO my_table (date_time) VALUES (CURRENT_TIMESTAMP)

Upvotes: 0

Maetschl
Maetschl

Reputation: 1339

If you use php, the correct format is:

date("Y-m-d H:i:s");

UPDATE: Minutes are expressed as i not m

Upvotes: 12

Vijin Paulraj
Vijin Paulraj

Reputation: 4648

An example of a PHP script which sets a date in MySQL manually,

<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')”;
mysql_query($query_date) or die(mysql_error());
?>

An example of a PHP script which sets a date in MySQL Automatic,

<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()',  CURDATE() )”;
mysql_query($query_date) or die(mysql_error());
?>

Upvotes: 0

Martti Laine
Martti Laine

Reputation: 12995

I would use function time() too, then it's easy to output different kind of timestamps with date().

$query = "INSERT INTO tbl VALUES (".time().");";

Upvotes: 1

preinheimer
preinheimer

Reputation: 3722

If you've got a timestamp in PHP check out FROM_UNIXTIME() http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime

$tstamp = time();

$query = "INSERT INTO `table` VALUES (FROM_UNIXTIME($tstamp))";

Upvotes: 4

Aly
Aly

Reputation: 16285

date("g") this will return 12-hour format of an hour without leading zeros. For more options see http://php.net/manual/en/function.date.php

Upvotes: 0

VolkerK
VolkerK

Reputation: 96159

INSERT ... (Now(), ...)

without additional quotes around the function Now()

Upvotes: 2

Related Questions