开发者

getdate() in mysql

开发者 https://www.devze.com 2023-01-07 07:46 出处:网络
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 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 th开发者_运维知识库e last few updates?


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']); ,


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消