Reputation: 577
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.
Upvotes: 0
Views: 2171
Reputation: 29434
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
Upvotes: 0
Reputation: 11096
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
Upvotes: 0
Reputation: 116110
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.
Upvotes: 1