10
How do I automatically save the day in the database without having to pass it when adding the product or whenever you add a product it automatically already writes the date of the computer in a table attribute.
10
How do I automatically save the day in the database without having to pass it when adding the product or whenever you add a product it automatically already writes the date of the computer in a table attribute.
13
As @Wallacemaxters said just add CURRENT_TIMESTAMP
in the field referring to the date:
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 upgrade the registration date is also updated.
It flashed, huh!
When making an Insert do not forget to enter the date
=NOW () which means now
@Megatron As Jorge suggests, you don’t need.
Thus, the bd "auto-inserts".
may seem silly but timestamp has a problem. It ends in 2038. I stress that the answer is not incorrect. It is a mere observation.
7
Leonardo, you can use the function getdate()
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
)
+1 did not know this function
@Wallacemaxters do not see this function being widely used too, I would like to know the reason rs...
I prefer to avoid these old functions... it is not wrong to use but those who know php and its history, know that functions like this go into "deprecated" mode in the near future.. a strong indication of this was the implementation of the Datetime() class a few years ago.
5
In the database, go to the Default column and put current_timestamp()
or now()
.
The field type should be timestamp
, date
or datetime
. Depends on your circumstance.
I should put this in the right default? but this giving ALTER TABLE error produtos
ADD data
DATE NOT NULL DEFAULT CURRRENT_TIMESTAMP ;
do not use date field with date name. because "date" is a reserved word from mysql.
Perform this: ALTER TABLE produtos ADD COLUMN data_reg TIMESTAMP NOT NULL DEFAULT current_timestamp();
+1 I think it’s a great idea to do this because you don’t need to control column insertion.
5
In my opinion, you should save the full date of today. If you need to filter the day, use specific functions of MYSQL
for that reason.
Defines your bank as CURRENT_TIMESTAMP
in the field referring to the date.
2
Assuming a field of type DATE/DATETIME/TIMESTAMP there are two forms.
1 - By the Database itself
Use functions like now() UPDATE tabela SET campo_data = now()
// You can also use CURRENT_TIMESTAMP, because now() is just a nickname for CURRENT_TIMESTAMP
or
Set the field with DEFAULT CURRENT_TIMESTAMP
and do not pass any value in INSERT
2 - By PHP
Use date functions like date() to generate a string in the YYYY-MM-DD format and put it 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’t imagine the reasons for this, but...) it is mandatory to use form 2
0
Create a datetime field, it will stick to the American format: Y-m-d H:i:s (Year-month-day Hour:Minutes:seconds):
Then, for insertion, run the query like this:
INSERT INTO tabela
(
campo1, campo2, seu_campo_datetime
)
VALUES (
'valor1', 'valor2', now()
);
And for updating:
UPDATE tabela
SET campo1='valor1',
campo2='valor2',
seu_campo_datetime=now()
WHERE 1;
And if you need to consult (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();
Browser other questions tagged php mysql
You are not signed in. Login or sign up in order to post.
saving the day reminded me of something like Superman saving the planet on any given day... saving date would be more appropriate.
– Daniel Omine