MYSQL How to keep the date data added? - php

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')

Related

CakePHP3, Saving current date on user registration

Using CakePHP3,I'm having trouble figuring out how to save the current DATE in (year, day, month) format. I'm using mySQL as the database, and I have a users table with a created_at column with type DATE. When a user registers, how do I save the current date along with all the other entity data? I've been looking around but the CakePHP Cookbook is a little unclear.
change the datatype of created_at to TIMESTAMP and set the default value to CURRENT_TIMESTAMP on the field settings.This should allow mysql to automatically set the current server date and time for you everytime a new record is created.
No code required!!!

How to Manually Adjust Value of timestamp To Previous Year?

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

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Recording time/date PHP form is inserted into SQL database?

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

Mysql 2 date type

Can I use 2 date type in 1 mysql table? because for some reason when I insert records for 2 different dates it doesnt work, only the first date is working the other one shows 00-00-00?
Thank you for your time and help
I will assume you are using a TIMESTAMP data type, and you want it to default to CURRENT_TIMESTAMP.
Only one TIMESTAMP column in a table can automatically be set on INSERT or UPDATE - and by default, MySQL goes with the first one.
If you would like to have more than one TIMESTAMP column get updated automatically on INSERT or UPDATE, you will need to turn to triggers.
The docs on the TIMESTAMP data type are a good read!
Edit: if this educated guess does not answer your rather vaguely-phrased question, you will probably want to update your question with an example of the queries you are running, and what exactly is happening/not happening afterward.

Categories