getdate() in mysql

2019-07-27 07:18发布

问题:

I am creating a system which updates the user activity when something is done. I have a variable $userhistory= 'User edited '.$info.' on July 14 2010 or (07-14-2010);

I want to know how can i get the date automatically. for sql query i am using NOW(), but in a variable like $userhistory how do i get the date and it want it only in either of these forms. not along with the time. Also, I am updating the column userhistory in the database, which is a text field. Is this the correct way to do it? How can i save only 5 or 10 of the last few updates?

回答1:

If you are using NOW() when you update or write an entry to the database, the column storing the date (userhistory?) should be of DATETIME type.

Then you'd you'd run your SQL as normal :

SELECT field1, field2, UNIX_TIMESTAMP(userhistory) FROM table;

Then in PHP, use date() on the database result to format it accordingly:

// July 14 2010
date('F j Y', $row['userhistory']);
// 07-14-2010
date('m-d-Y', $row['userhistory']); ,


回答2:

You can do it without PHP as well.

SELECT
    DATE_FORMAT(date_column, '%M %d %Y') AS 'Formatted',
    DATE_FORMAT(date_column, '%m-%d-%Y') AS 'Formatted2'
FROM
    table;