How to save the day in the bank?

10

How do I automatically save the day in the database without having to spend it when adding the product, ie every time you add a product, it automatically writes the date of the computer to a table attribute.

    
asked by anonymous 08.10.2015 / 18:30

6 answers

13

As I said @WallaceMaxters just add CURRENT_TIMESTAMP to the date field:

CREATE TABLE table_name (
  data TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

The ON UPDATE CURRENT_TIMESTAMP can be used if you also want the update to register the date is also updated.

    
08.10.2015 / 18:48
7

Leonardo, you can use the getdate() function of PHP .

Example taken from phpnet :

<?php
  $today = getdate(); 
  print_r($today);

The result obtained in $today is similar to this:

Array
(
    [seconds] => 40
    [minutes] => 58
    [hours]   => 21
    [mday]    => 17
    [wday]    => 2
    [mon]     => 6
    [year]    => 2003
    [yday]    => 167
    [weekday] => Tuesday
    [month]   => June
    [0]       => 1055901520
)
    
08.10.2015 / 18:33
5

In the database, it goes into the Default column and places current_timestamp() or now() .

The field type must be timestamp , date or datetime . It depends on your circumstances.

    
08.10.2015 / 18:32
5

In my opinion, you should save today's full date. If you need to filter the day, use specific functions of MYSQL for this.

Define your bank as CURRENT_TIMESTAMP in the date field.

    
08.10.2015 / 18:32
2
Assuming a field of type DATE / DATETIME / TIMESTAMP there are two ways.

1 - By the Database itself   Use functions like now () UPDATE tabela SET campo_data = now() // You can also use CURRENT_TIMESTAMP, since now () is only a nickname for CURRENT_TIMESTAMP     or   Set the field with DEFAULT CURRENT_TIMESTAMP and no value in INSERT

2 - By PHP    Use date functions such as date () to generate a string in YYYY-MM-DD format and place in the SQL statement    Ex: $dt = date('YYYY-MM-DD H:i:s');

Note: If by chance the field is of type CHAR / VARCHAR / TEXT (I can not imagine the reasons for this, but ...) it is mandatory to use the form 2

    
08.10.2015 / 18:48
0

Create a datetime field, it will look like the American format: Y-m-d H: i: s (Year-month-day Time: Minutes: seconds):

Then, for insertion, execute the query like this:

  INSERT INTO tabela
  (
   campo1, campo2, seu_campo_datetime
  )
  VALUES (
  'valor1', 'valor2', now()
  );

And to upgrade:

  UPDATE tabela
  SET campo1='valor1',
      campo2='valor2',
      seu_campo_datetime=now()
  WHERE 1;

And if you need to query (you can format the output), do so:

SELECT DATE_FORMAT(seu_campo_datetime,'%d/%m/%Y') as DataBanco 
from   tabela;

If you prefer to use a default date, it has to be in timestamp format:

ALTER TABLE produtos 
ADD COLUMN data_reg TIMESTAMP NOT NULL
DEFAULT current_timestamp();
    
08.10.2015 / 21:07