I'm writing a PHP/MySQL
program. I need to add the current date each time a new record to the TABLE
is added. I have a field called DATE_ADDED
. I'm confused how to use CURDATE()
function from MySQL
to write this to the TABLE. Or should I from PHP
use a date function to get today's date and pass it as a variable to be written to the TABLE? I don't need a TIMESTAMP
, just YYYY-MM-DD
. Thanks!
You have to try with php like
$today = date("Y-m-d");
and then while adding data to your db give this value with the remaining
You can set it as the default value for that column date_added
in the table definition like so:
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
INSERT INTO table_name ( field1, field2,...DATE_ADDED )
VALUES
( value1, value2,...CURDATE());
$regdate=date('Y-m-d');
$sql = "INSERT INTO table_name (fld_name1, fld_name2,fld_regdate) VALUES ('value1', 'value2', '$regdate')";
$rs = mysql_query($sql) or die('ERROR:' mysql_error());
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();