简体   繁体   中英

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.

My php code:

#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. I tried using different formats(d/m/Y etc.), but didn't work. I have checked that in MySQL table, date column's type is date, not string. I am a newbie in php and MySQL and I don't know why this is happening.

Also, I want this page to daily(at 11:59 PM) send mail of that day's entries. 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 .

Use NOW() function of MySQL with column type DATETIME

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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