Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert current date to the database?

Tags:

php

mysql

How do I insert the current date to my database? I have a column called date to put it on. I want to insert it at the same time I insert this:

$sql="INSERT INTO `Lines` (Text, PID, Position)
VALUES
('$text','$pid','$position')";

Is there a way to automate it in PHPMyAdmin or it's the same to do it this way? Thanks

like image 262
lisovaccaro Avatar asked Jul 30 '11 01:07

lisovaccaro


3 Answers

If the table definition has the timestamp column default set to CURRENT_TIMESTAMP, you actually don't have to do anything at all. Otherwise, NOW() and CURRENT_TIMESTAMP will work, as in:

INSERT INTO t1 (timestamp_column) VALUES (NOW());

There is a difference between CURRENT_TIMESTAMP and NOW() but it's probably too small to matter to you.

phpMyAdmin seems like it has CURRENT_TIMESTAMP as an option when creating a new column.

like image 175
Explosion Pills Avatar answered Nov 15 '22 01:11

Explosion Pills


INSERT INTO `Lines` (`date`) VALUES (NOW());
like image 36
deceze Avatar answered Nov 14 '22 23:11

deceze


Depending on your requirement, you can also do this

$date=date('d.m.y h:i:s');

And then insert $date. I mean if you only want to view the date & time. Otherwise i also recommend time().

like image 45
Vinayak Garg Avatar answered Nov 15 '22 01:11

Vinayak Garg