faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

116 of 121 people (96%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I store the current date in a MySQL datetime field?

Apr 19th, 2001 17:32
Onno Benschop, Jasja de Vries, Acebone,


This inserts the time on the PHP server into the database:
    $date = date("Y-m-d G:i:s") ;
    mysql_db_query( "NAME_OF_DATABASE", "INSERT INTO 
      name_of_table (name_of_datetime_column) 
      VALUES ('$date')" ) ;
You might also consider the following approach which inserts the time 
on the MySQL server into the database:
    mysql_db_query( "NAME_OF_DATABASE", "INSERT INTO 
      name_of_table (name_of_datetime_column) 
      VALUES (NOW())") ;
Please note that NOW() is a MySQL variable. Also note that MySQL is 
Case-sensitive -> now() or Now() won't work! 
In the second example there is no need to set up a $date PHP-variable.
If you were to run the same code side by side, you may find a 
microsecond or so difference in the database records, because the time 
that the PHP date() command is run is different from the time the MySQL 
NOW() command is run.