Insert current date into database?

How to insert current date into my database? I have a column called date to include it. I want to paste it at the same time when I paste this:

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

Is there a way to automate it in PHPMyAdmin or does it also do it this way? Thanks

+6
source share
5 answers

If the default timestamp column is set to CURRENT_TIMESTAMP in the table definition, you 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 this is probably too little for you.

phpMyAdmin seems to have CURRENT_TIMESTAMP as an option when creating a new column.

+7
source
 INSERT INTO `Lines` (`date`) VALUES (NOW()); 
+4
source

You can also do this depending on your requirement.

 $date=date('dmy h:i:s'); 

And then insert $ date. I mean, if you only want to see the date and time. Otherwise, I also recommend time ().

+1
source

Otherwise ... if you use an integer, as is often the case in PHP, just use time() and insert it just like you inserted other variables, or use MySQL UNIX_TIMESTAMP()

0
source

The best way to store timestamps is with UNIX timestamp integers in GMT / UTC. This allows you to always know the exact time no matter where you are, your server or your users. The bonus is that you can let your users set their time zone and display time that is meaningful to them.

 $sql = "INSERT INTO `Lines` (`timestamp`) VALUES ('" . time() . "')"; 

or

 $sql = "INSERT INTO `Lines` (`timestamp`) VALUES ( UNIX_TIMESTAMP() )"; 

Be careful if you decide to use NOW () or CURRENT_TIMESTAMP, as they are controlled by the database server and its settings. If the server on which your site is hosted is in one time zone and you are switching to another host in a different time zone, all your timestamps will be incorrect. Using Unix integers will add a bit of extra effort wherever you apply the application, but it gives you maximum accuracy and maximum flexibility.

0
source

Source: https://habr.com/ru/post/893920/


All Articles