以日期时间格式mySQL插入当前日期

时间:2012-03-02 22:06:53

标签: php mysql

我在将日期正确插入数据库时​​遇到问题。

$date = date('m/d/Y h:i:s', time());

我使用这种格式,并且它正确地回显,但是,当我插入

mysql_query("INSERT INTO table 
(dateposted) 
VALUES ('$date')");

它似乎无法正常工作,并且时间仍为00:00:00 如果你能找到很棒的解决方案,谢谢。

15 个答案:

答案 0 :(得分:193)

如果您希望存储当前时间,只需使用MYSQL的功能。

mysql_query("INSERT INTO `table` (`dateposted`) VALUES (now())");

如果您需要使用PHP来执行此操作,请使用Y-m-d H:i:s格式尝试

$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$date')");

答案 1 :(得分:34)

试试这个

$date = date('Y-m-d H:i:s');

答案 2 :(得分:6)

NOW()用于在MySQL表中插入当前日期和时间。数据类型为DATETIME, DATE, TIME & TIMESTAMP的所有字段都适用于此函数。

YYYY-MM-DD HH:mm:SS

演示:

以下代码显示了NOW()

的用法
INSERT INTO auto_ins
(MySQL_Function, DateTime, Date, Time, Year, TimeStamp)
VALUES
(“NOW()”, NOW(), NOW(), NOW(), NOW(), NOW());

答案 3 :(得分:4)

你可以使用CURRENT_TIMESTAMP,mysql函数

答案 4 :(得分:3)

将名为type的列的dateposted设置为DATETIME并运行以下查询:

INSERT INTO table (`dateposted`) VALUES (CURRENT_TIMESTAMP)

答案 5 :(得分:1)

datetime”希望日期格式如下:YYYY-MM-DD HH:MM:SS

所以请在插入时格式化您的日期。

答案 6 :(得分:0)

     <?php $date= date("Y-m-d");
$time=date("H:m");
$datetime=$date."T".$time;
mysql_query(INSERT INTO table (`dateposted`) VALUES ($datetime));
?>

<form action="form.php" method="get">
<input type="datetime-local" name="date" value="<?php echo $datetime; ?>">
<input type="submit" name="submit" value="submit">

答案 7 :(得分:0)

我相信,您需要像下面的数据库字段类型一样更改您的代码。

mysql_query("INSERT INTO table (`dateposted`) VALUES ($datetime)");

希望,将完美地运作。

答案 8 :(得分:0)

如果您从PHP传递日期,则可以使用STR_TO_DATE() mysql函数使用任何格式。 让你通过html表格插入日期

$Tdate = "'".$_POST["Tdate"]."'" ;    //   10/04/2016
$Tdate = "STR_TO_DATE(".$Tdate.", '%d/%m/%Y')"  ;  
mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$Tdate')");

dateposted应为mysql date类型。或者mysql会添加00:00:00
在某些情况下,您最好将日期和时间一起插入数据库,以便您可以使用小时和秒进行计算。 ()。

$Tdate=date('Y/m/d H:i:s') ; // this to get current date as text .
$Tdate = "STR_TO_DATE(".$Tdate.", '%d/%m/%Y %H:%i:%s')"  ;  

答案 9 :(得分:0)

使其更容易和更有效的最佳方法是:

CREATE TABLE table_name (
field1,
..
..
time_updated timestamp default current_timestamp
)

现在,当您在表格中插入一行时,可以跳过此字段(time_updated),因为它会将当前时间填充为默认值

答案 10 :(得分:0)

只需使用您的时区:

date_default_timezone_set('Asia/Kolkata');      
$date=date("Y/m/d h:i:sa");

答案 11 :(得分:0)

$ date = date('Y-m-d H:i:s'); 类型为:“ datetime”对我来说非常有效,因为我想打印整个日期和时间戳。

$ date = date('Y-m-d H:i:s'); $ stmtc-> bindParam(2,$ date);

答案 12 :(得分:0)

这取决于您为数据库表设置的数据类型。

DATETIME (数据类型)

MySQL数据库
INSERT INTO t1 (dateposted) VALUES ( NOW() )
// This will insert date and time into the col. Do not use quote around the val
的PHP
$dt = date('Y-m-d h:i:s');
INSERT INTO t1 (dateposted) VALUES ( '$dt' )
// This will insert date into the col using php var. Wrap with quote.

日期(数据类型)

MySQL数据库
INSERT INTO t1 (dateposted) VALUES ( NOW() )
// Yes, you use the same NOW() without the quotes.
// Because your datatype is set to DATE it will insert only the date
的PHP
$dt = date('Y-m-d');
INSERT INTO t1 (dateposted) VALUES ( '$dt' )
// This will insert date into the col using php var.

TIME (数据类型)

MySQL数据库
INSERT INTO t1 (dateposted) VALUES ( NOW() )
// Yes, you use the same NOW() as well. 
// Because your datatype is set to TIME it will insert only the time
的PHP
$dt = date('h:i:s');
INSERT INTO t1 (dateposted) VALUES ( '$dt' )
// This will insert time.

答案 13 :(得分:-1)

使用此

$date = date('m/d/Y h:i:s', time());

然后 在MYSQL中做

type=varchar

然后将成功插入日期和时间

答案 14 :(得分:-5)

试试这个

- (void)startAuthenticatingWithCompletion:(void (^)(BOOL success, NSError *error))completion{
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;

    if (success) {
        completion(YES, nil);
        //[self performSegueWithIdentifier: @"Success" sender:nil];

    } else {
        completion(NO, authError);
    }
}