Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

put timestmap into SQL datetime field

Status
Not open for further replies.

DaveC426913

Programmer
Jul 28, 2003
274
CA
This is so basic I'm having trouble finding a basic description.

I just want to put the current datetime (i.e. now) into a field in the db so I know when the record was created.

 
Hello DaveC426913,

I am not sure if this is what you are looking for but, If this is for a databse, you should fisrt you should define in your database a field for time. Then in your sql statement do something like this.

$sql = "INSERT INTO database_table time vaules(now());

if you are just referring to php coding then go here and read and look at the bottom for examples

imryn
 
if the db field is set to timestamp (in mysql) and you do not supply any value to it on insert and update actions, mysql will insert the current time for each action.

if you want it to be a static field (not changing each time the field updats) then i'd recommend using unix timestamps and a numeric field type.
Code:
$sql = "insert 
        into DBTABLE 
        set createtime='".strtotime("now").'";

and if you want a human readable date then i would use a field type of datetime and something almost identical to imryn's suggestion:
Code:
$sql = "insert 
        into DBTABLE 
        set createtime=now()";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top