SQL Database Timestamp - php

I have a database where people register on a website and their form data then gets inserted into their database. Along with all that information, I want to insert the time of their registration. For example I have an auto-incrementing ID column which I do nothing in my php script for, the sql database automatically increments that with every new entry. In the same way, can I have a time column that I don't have to do anything in the script for, rather the database will just get the current time and put that along with the other inserted information? I'm using phpMyAdmin and I tried adding a column named Time with the type as DATETIME and the default value as the CURRENT_TIMESTAMP but it woulnd't let me add that and said invalid default value for Time.

Change the column type to TIMESTAMP.

Use a timestamp field, which does exactly what you want.

use timestamp column type e.g.
`inserted` timestamp NOT NULL default CURRENT_TIMESTAMP

Related

"Casting" MySQL TIMESTAMP values to a DATETIME column: any gotchas?

I have a system where, from time to time, the code will INSERT a new value in a given table. Let's say that table has these 2 columns:
ID: int(11)
Date: TIMESTAMP
And the "Date" column is defined to have the CURRENT_TIMESTAMP as default value. So, from time to time, the code does a:
INSERT INTO tabla (ID) VALUES ('$bla');
And we get the new value and the TIMESTAMP when it was inserted.
Now, I want to get that TIMESTAMP value and insert it into another table's DATETIME column (this is a legacy system that I'm hacking, and I can't/rather not touch this other table's definitions).
My question is: can I just take that TIMESTAMP value as-is and insert it in the other table's DATETIME column without any gotchas? From what I've read, the main difference between both types is in timezone handling, but in my case:
Both tables are in the same database.
There's only one copy of the DB (it's not like we are replicating DB's from different countries).
The dates inserted all originate from the same country.
I'm using MySQL 5.5 and PHP.
My question is: can I just take that TIMESTAMP value as-is and insert it in the other table's DATETIME column without any gotchas?
Yes.
You've correctly observed that the DATETIME values will be recorded with reference to the currently chosen timezone.

MySQL 5.5 automatic timestamp update to current

So the timestamp containing column in my database gets automatically updated to current timestamp when I update data there, even if I don't pass any timestamps (not passing null, but just ignoring this field, not including it in update array).
I have checked table structure and discovered that the timestamp column settings are set to "default - CURRENT_TIMESTAMP" and "on update - CURRENT_TIMESTAMP", but I've never written statements like these in my migrations and I don't need any automatic update to current timestamp (in my migration files, from which the database was created, there are just rows like $table->timestamp('date');).
And so here is the question: what should be written in migration files to avoid this situation and why does it even appear? Is this some kind of MySQL feature, or does MySQL interpret ignored field like passed null?
Well I found solution in MySQL docs, it is said that no specifications in migration is equal to specifying current_timestamp on default and update, so to avoid my issue I have to specify default value in migration, and do not specify update attribute.
Source: http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html

redbeanphp : How to use mysql ON UPDATE timestamp

I need mysql to automatically update a field on every update. I created the field as:
`lastModified` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
But the problem is as I update a record using R::store, the lastModified does not get updated automatically but is set to the last set value. Note that my code is not setting the field. Basically doing a findOne and store to set any other field should update the timestamp but looks like redbeans is reading the whole row (so lastUpdated is filled in as old time) and assuming when I store, it thinks I am setting it to old time value.
lastModified property is pure MYSQL. It does not depend on the implementation.
RedbeanPHP also updates mysql database, have you disabled this?
R::freeze( TRUE );
Once you put it on freeze, you can modify the database and tables yourself, and redbean will not update it. This should also take care of the lastModified property.
try this
$bean->lastModified = date('Y-m-d G:i:s');
R::store($bean);

MySQL insert date if no field exists

Is there a way in MySQL to get insert date/time for rows if there was no insert_date field. I have a database which I configured to store insert_date but can I populate the field before that change ( month ago ). Is that even possible?
Pull that insert date/time from log or something else?
Nope. If that date wasn't stored before, it's impossible to find out when a row was inserted. The best you can do is just pick a date, or maybe make a rough estimate if you have information to base that on (for instance, the create date of a customer might be related to the date of their first invoice)..
By the way, you can add timestamp columns and specify the clause DEFAULT CURRENT_TIMESTAMP to set a timestamp as soon as you insert the row. That way, you don't need a trigger to update the row.
You can even add a clause ON UPDATE CURRENT_TIMESTAMP so the column (or a different column) is updated automatically too.
See Timestamp initialization for more information about this subject.
This doesn't change the fact, though, that you cannot get those values for rows that already exist.

Submitting current timestamp in CakePHP

What is the method to submit a current timestamp directly on an INSERT or an UPDATE? If I were running regular SQL, I would use the function NOW() for the specific SQL field on submission. How would I do that with CakePHP?
$this->Model->save($this->data)
In CakePHP, you can include the NOW() function unescaped by using DboSource::expression
$this->data['SomeModel']['your_datetime_field'] = DboSource::expression('NOW()');
$this->Model->save($this->data);
This is the preferred way of including MySQL functions in your saves.
http://api.cakephp.org/2.3/class-DboSource.html#_expression
if you add the created and modified columns in you table they will be automatically populated with current time stamp. If the case is different - i.e. you want to populate a field which later on you want to modify, probably using the edorian's solution is best.
You can set timestamp field to auto initialize and auto update
timestampfield TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

Categories