简体   繁体   English

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

[英]How to insert today's date in mysql query?

I'm using PHP and MySQL. 我正在使用PHP和MySQL。 My sql query is as follows: 我的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()) )  

I want to store todays date in UNIX Timestamp format. 我想以UNIX时间戳格式存储今天的日期。 If I execute above query I'm getting 1970 stored into the table. 如果执行上面的查询,我将1970存储到表中。 Actually I want to store UNIX timestamp equivalent of today's date. 实际上,我想存储与今天的日期等效的UNIX时间戳。 Can anyone help me in this regard? 有人可以在这方面帮助我吗? Thanks in advance. 提前致谢。

Use NOW() function in your my sql query 在我的SQL查询中使用NOW()函数

NOW() , CURDATE() , CURTIME() NOW()CURDATE()CURTIME()

NOW() usually works in SQL statements / queries and returns the date and time. 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() )  

Alternatively, you can also use the PHP's built-in time() function. 另外,您也可以使用PHP的内置time()函数。

$timestamp = time(); //1374821956

Hope this helps! 希望这可以帮助!

You just have to use unix_timestamp() function in order to achieve this. 您只需使用unix_timestamp()函数即可实现此目的。 This will automatically insert UNIX timstamp equivalent for todays date. 这将自动插入与今天相同的UNIX timstamp。 The corrected query will be as follows: 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()) 更正后的查询将如下所示: 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())

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

Try this

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

I would use this. 我会用这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM