如何在mysql查询中插入今天的日期?

时间:2013-07-26 06:53:19

标签: php mysql sql

我正在使用PHP和MySQL。我的SQL查询如下:

INSERT INTO ocn.newsletter
            (newsletter_name,
             newsletter_subject,
             newsletter_email_body,
             newsletter_call_to_action_status,
             newsletter_call_to_action_text,
             newsletter_call_to_action_link,
             newsletter_created_date)
VALUE      ('Ramzan ID',
            'Ramzan Greetings',
            'Happy Ramzan Eid to All',
            '0',
            '',
            '',
            From_unixtime(Curdate()) )  

我想以UNIX时间戳格式存储今天的日期。如果我执行上面的查询,我将1970年存储到表中。实际上我想存储相当于今天日期的UNIX时间戳。谁可以在这方面帮助我?提前谢谢。

6 个答案:

答案 0 :(得分:1)

在我的sql查询中使用NOW()函数

NOW()CURDATE()CURTIME()

答案 1 :(得分:1)

NOW()通常在SQL语句/查询中起作用并返回日期和时间。

INSERT INTO ocn.newsletter
            (newsletter_name,
             newsletter_subject,
             newsletter_email_body,
             newsletter_call_to_action_status,
             newsletter_call_to_action_text,
             newsletter_call_to_action_link,
             newsletter_created_date)
VALUES      ('Ramzan ID',
            'Ramzan Greetings',
            'Happy Ramzan Eid to All',
            '0',
            '',
            '',
            NOW() )  

或者,您也可以使用PHP的内置time()函数。

$timestamp = time(); //1374821956

希望这有帮助!

答案 2 :(得分:1)

您只需使用unix_timestamp()函数即可实现此目的。这将自动为今天的日期插入等效的UNIX timstamp。更正后的查询如下: INSERT INTO OCN.newsletter(newsletter_name, newsletter_subject, newsletter_email_body, newsletter_call_to_action_status, newsletter_call_to_action_text, newsletter_call_to_action_link, newsletter_created_date) VALUE('Ramzan ID', 'Ramzan Greetings', 'Happy Ramzan Eid to All', '0', '', '', unix_timestamp())

答案 3 :(得分:0)

INSERT INTO OCN.newsletter(newsletter_name, newsletter_subject, newsletter_email_body, newsletter_call_to_action_status, newsletter_call_to_action_text, newsletter_call_to_action_link, newsletter_created_date) VALUE('Ramzan ID', 'Ramzan Greetings', 'Happy Ramzan Eid to All', '0', '', '', NOW())

如果使用MYSQL,可以将“newsletter_created_date”的默认值设置为TIMESTAMP,当您执行插入时,会自动设置TIMESTAMP

答案 4 :(得分:0)

Try this

$query_auto = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )";

答案 5 :(得分:0)

我会用这个。

  $sDate = date("Y-m-d H:i:s");
  mysql_query("insert into `ocn.newsletter` set `newsletter_created_date` = '$sDate'");