简体   繁体   English

在MySQL数据库中存储今天的日期

[英]Storing Today's Date in MySQL Database

When my html form is submitted, I want today's date also stored in one column along with the data given by the form in mysql table. 当我提交html表单时,我希望今天的日期也与mysql表中表单提供的数据一起存储在一栏中。

My php code: 我的PHP代码:

#some code
$date = date('d-m-Y');
#some code
$sql = "INSERT INTO table1(rollNo, password, name, item, place, description, contact, date) VALUES('$rollNo', '$password', '$name', '$item', '$place', '$description', '$contact', '$date') ";  

But for some reason, every time form is submitted, in the date column '0000-00-00' is stored instead of today's date. 但是由于某种原因,每次提交表单时,都会在日期列'0000-00-00'中存储而不是今天的日期。 I tried using different formats(d/m/Y etc.), but didn't work. 我尝试使用其他格式(d / m / Y等),但是没有用。 I have checked that in MySQL table, date column's type is date, not string. 我已经检查过,在MySQL表中,日期列的类型是日期,而不是字符串。 I am a newbie in php and MySQL and I don't know why this is happening. 我是php和MySQL的新手,我不知道为什么会这样。

Also, I want this page to daily(at 11:59 PM) send mail of that day's entries. 另外,我希望该页面每天(晚上11:59)发送当天输入的邮件。 For that, I am planning to check every entry's date with today's date, and send mail of only those that match. 为此,我计划将每个条目的日期与今天的日期进行检查,并仅发送匹配的邮件。 Please tell me if there is another simpler method of doing it. 请告诉我是否还有另一种更简单的方法。

EDIT: Just to make it clear, date column's type is DATE . 编辑:为了清楚起见,日期列的类型为DATE

Use NOW() function of MySQL with column type DATETIME 使用列类型为DATETIME的MySQL的NOW()函数

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now

You can also get just the date: 您还可以获取日期:

http://www.w3schools.com/sql/func_curdate.asp http://www.w3schools.com/sql/func_curdate.asp

UPDATE table SET date = CURDATE();

Use: 采用:

`dateandtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

And using a simple substring, you can pick out the date (YYYY-MM-DD) with ease. 使用简单的子字符串,您可以轻松选择日期(YYYY-MM-DD)。

substr($sql['dateandtime'], 0, 10);

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

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