Clarkeez Posted July 3, 2011 Share Posted July 3, 2011 In my website, I have created a forum like system where people post etc.. I'm trying to think of the best way for the script to know how fresh the topic is (i.e. how long ago was the last post) and thus place it at the top In other words, I have made a column im mysql called 'lastpost' and i don't know whats the best php function to use.. can you help? Quote Link to comment https://forums.phpfreaks.com/topic/241020-php-mysql-best-function-for-last-post-time/ Share on other sites More sharing options...
jcbones Posted July 3, 2011 Share Posted July 3, 2011 Why not make lastpost a timestamp column in mysql, then just order it by the lastpost? Quote Link to comment https://forums.phpfreaks.com/topic/241020-php-mysql-best-function-for-last-post-time/#findComment-1237984 Share on other sites More sharing options...
Clarkeez Posted July 3, 2011 Author Share Posted July 3, 2011 I dont know how to do it :/ I've tried settings lastpost column to DATETIME and TIMESTAMP but when i insert something, it stays NULL. Quote Link to comment https://forums.phpfreaks.com/topic/241020-php-mysql-best-function-for-last-post-time/#findComment-1237990 Share on other sites More sharing options...
jcbones Posted July 3, 2011 Share Posted July 3, 2011 There are two ways to do this: 1. Use the mysql function NOW() to insert the current timestamp into a timestamp column. INSERT INTO table (column1,column2,lastpost) VALUES ('column','column',NOW()); 2. You can set the column to default to the current timestamp. This will eliminate the need to tell mysql to put anything in that column. CREATE TABLE IF NOT EXISTS `table` ( `column1` varchar(20) NOT NULL, `column2` varchar(20) NOT NULL, `lastpost` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ); My suggestions at least. Quote Link to comment https://forums.phpfreaks.com/topic/241020-php-mysql-best-function-for-last-post-time/#findComment-1237996 Share on other sites More sharing options...
mikesta707 Posted July 3, 2011 Share Posted July 3, 2011 jcbones explained it very well, but in the future you should probably post the code you are trying to use that is giving you trouble. Also, doing a little research on the proper technique to use is always a good way to get an answer. In your case, simply searching "mysql timestamp tutorial" or something like that would have probably yielded some useful results Quote Link to comment https://forums.phpfreaks.com/topic/241020-php-mysql-best-function-for-last-post-time/#findComment-1238004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.