user1592953
user1592953

Reputation: 135

Storing the current date/time

I have a site where users can update their status. Knowing the date/time they posted the status is very important. I create a field in my table and gave it the date time type. I am new to working with dates/times. I know the structure for the datetime field is this:

YYYY-MM-DD HH:MM:SS 

I thought doing this in my INSERT query would work, but It didnt.

    $query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story', 
'date()', '1')";

When I check the database table the post is successful, but the datetime reads

0000-00-00 00:00:00

What can I do to achieve my end result. This whole unix time stamp and stuff with date/time really confuses me. Thanks.

Upvotes: 1

Views: 123

Answers (3)

GautamD31
GautamD31

Reputation: 28753

Lets tr this:

$date_time = NOW();

then

$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story',$date_time, 'My_View')";

Upvotes: 0

user1629095
user1629095

Reputation: 33

Give this a try, this will add the record to the database with the current date and time in the format you posted:

$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story', now(), '1')";

EDIT: Posted same time as above.

Upvotes: 2

xdazz
xdazz

Reputation: 160933

Don't quote the mysql function, and you should use NOW() instead of DATE().

$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story', NOW(), '1')";

Upvotes: 3

Related Questions