I am using timestamp to store the pubdate of uploads. I want to add a filter to sort records by the year they were uploaded so need to change a couple of records to previous years for testing purposes. When I try to manually adjust the date from within mysql by clicking the calendar and picking a date it flags errors.
The value of timestamp is set to CURRENT_TIMESTAMP.
The values of function in the timestamp field are :
NOW
CURDATE
FROM_DAYS
FROM_UNIXTIME
PERIOD_ADD
PERIOD_DIFF
TO_DAYS
I cannot find anything online relating to this. I don't need to perform calculations I need to manually adjust the date.
In case the problem is that you used an ON UPDATE CURRENT_TIMESTAMP on this column you can remove this setting by redefining your column:
ALTER TABLE yourTable MODIFY COLUMN pubdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
You might also check the docs:
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP,
it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and
ON UPDATE CURRENT_TIMESTAMP.
And I guess your table was created without defining any of those two resulting in the behavior that you cannot update this column as it is updated automatically.
What happens is:
You pick a row and overwrite the timestamp (like setting the date to 2013)
MySQL detects that the row was updated
as ON UPDATE CURRENT TIMESTAMP is implicitly specified MySQL updates the timestamp field with the current timestamp
Your changes to the timestamp will be overwritten / lost
Related
I am new with MYSQL and PHP integration of it. So I am trying to make an Index where you can add websites and script shows the URL - Title - Last Updated Date. Till here I have no problem but I also want to keep the Date/Time the website added to the database. I am currently using TIMESTAMP but it changes the data everytime the websites updates.
Is there anything I can use for showing and storing the date when the data added?
Your timestamp is currently being updated on every UPDATE. I assume it is probably defined like this:
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
But you don't need the last "ON UPDATE CURRENT_TIMESTAMP"
Just this definition will automatically set the default value to the time the row is created, and it wont change.
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Use a Date type on the mysql table and
when you create the record just fill the date field with php
new Datetime('now');
when you update a record do not enter anything in that field
UPDATE table SET ULR='new_url', (and all the other field but date added) Where something=something
when you retrive the date from Mysql use the format method
$date->format('Y M D hh:mm')
I am trying to update a MySQL table with an 'expiration date'. I've collected a timestamp value for all my rows as people have registered into the table but now I want to create an expiration date relative to that timestamp (not to relative to the current time). Here's my code:
$timestamp = $row['timestamp'];
$sql_update = "
UPDATE jobs
SET expiration_date = DATE_ADD('$timestamp',INTERVAL 56 DAY)";
$result_update = $mysqli->query($sql_update) or die($mysqli->error);
Understand that this is being run in a 'while' loop so it's running through each row of the table an updating them as it goes. The issue is that I ran it once and all it did was update the 'expiration_date' row to 56 days from NOW not 56 days from the timestamp's value. The timestamp is set on CURRENT_TIMESTAMP for when the a new entry is registered which I'm assuming is the problem. I've echoed out $timestamp to troubleshoot and it echoes out the correct value (not the current time) when I echo it but when it goes to actually update the expiration date it seems to be drawing from the fact that it's a CURRENT_TIMESTAMP. Is there a way to explicit query for the value of the timestamp?
I'm hoping to find a way that doesn't involved restructuring the database. I know I could have it so instead of a timestamp row, I could make it a datetime row and set it to the value of NOW() when database is being initially queried to add a row but I'd prefer to find a solution within the way the table is currently set up. Thanks!
Probably somehow the value in $timestamp is wrong.
Try adding the timestamp directly in the query using the column name. E.g. : DATE_ADD(timestamp,INTERVAL 56 DAY)
What you are currently doing, hardly makes any sense. You first get the timestamp from the database and assign it to the $timestamp variable, only to use it in the query again..
In situations like these, you can better directly use the column name to access the data in your query.
I wonder why are you running this query in loop ?
You can use only one sql statement. Update from select;
update jobs a set a.expiration_date = DATE_ADD(a.timestampFieldName,INTERVAL 56 DAY), a.timestampFieldName = a.timestampFieldName;
I supose that there is some "bug" (don't know) in MySQL because if you don't add a.timestampFieldName = a.timestampFieldName then this field will be set to current timestamp;
I'm looking to order certain uploads by time order arrived (descending), but I'm not sure how I would record the time and date of submission using PHP/SQL. I'm pretty new to this, learning by coming up with projects and working through them as best as I can. Thanks for whatever help you can give me.
EDIT: I understand that the those functions exist, I just have no idea how I would implement them.
MySQL supports TIMESTAMP fields that can be automatically updated when the record is updated.
When you specify DEFAULT CURRENT_TIMESTAMP, the current time is inserted in new records. When you specify ON UPDATE CURRENT_TIMESTAMP as well, the timestamp is updated when the record is updated. That way, MySQL can automatically log the time for you.
You can do this using MySQL's NOW function.
Also see MySQL's Date and Time functions : http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
You have to create a column for saving the date and time.
I often use the following two columns:
created DATETIME DEFAULT NOW(),
modified DATETIME DEFAULT NOW()
That's all. If you insert a new record, MySQL will automatically update created and modified to the current date and time.
And if you want to order all uploads by their creation date:
SELECT * FROM uploads ORDER BY created DESC
Also see: http://sql-info.de/mysql/examples/CREATE-TABLE-examples.html#1_5
In my table I have a dateCreated column in the format of DATETIME.
How do I insert the current datetime when I add the record to the database?
Better use TIMESTAMP instead and use CURRENT_TIMESTAMP to get current timestamp. TIMESTAMP should behave exaclty (or very similar) to DATETIME so you shouldn't notice a difference.
Furthermore you may set CURRENT_TIMESTAMP as default for all new entries.
Use the CURDATETIME()
How do you add time or days to a current utc_timestamp?
I am using;
new CDbExpression('UTC_TIMESTAMP()')
for both 'created' and 'updated' fields in my mysql table but would like to add an 'expiry' field which would allow 4 days from creation date. I presume this is possible but am unsure how.
for insert/update current time
UPDATE table
SET created = NOW()
for 4 days from creation date
SELECT * FROM table WHERE created > DATE_SUB( NOW( ), INTERVAL 4 DAY )
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
In MySQL :
ALTER TABLE `table`
ADD expiry datetime DEFAULT DATE_ADD( utc_timestamp( ) , INTERVAL 4 DAY);
"The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression. This means,
for example, that you cannot set the default for a date column to be
the value of a function such as NOW() or CURRENT_DATE."
So, this is explicitely documented limitation
you have to create TRIGGER if your MySQL Version < 5.6.5
BUT
MySQL 5.6.5 changelog stats
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table.
Reference :
http://bugs.mysql.com/bug.php?id=27645
http://optimize-this.blogspot.in/