OK I store the publish date of user submitted content in the DB like this: 0000-00-00 which is basically date(Y-m-d); I want to give the user the option to let it expire 24 hours later 1 week later 1 Month later How would you go about setting an expiration date to store in the DB alongside the published date? I can change the format I store the date if need be. I'd imagine I need to add an actual time stamp for the 24 hour part right? Of course Green Rep for anyone who even tries to help
I would add simply an expiration date time value in seconds to the table. When the user selects an expiry period, I would convert that to seconds. I would add that the published date (in seconds) and enter that in the expiration date cell. Then, when displaying articles, I would check to see whether it has expired. If the current time stamp value (in seconds) is greater than the value in that cell than it has expired, otherwise it has not. If you cannot change that table, then you would create another one which contains the ID for the story and the expiry time. You would then make the comparison, searching for records which match the story ID and where the expiry time is greater than the current time
How code I do that in seconds though? And hwo would I store the original timestamp in the DB so it could accurately be read and calculated? Y-m-d H-m-s ? Maybe something like $today = date("Y-m-d", time()); // Define timestamp for expiration time $exp_time = time()+86400; That would be 24 hours so then I could store the expiration stamp like $exp = date("Y-m-d", $exp_time); ? 1 week = 604800 seconds 1 month = 2629743.83 seconds
hi George, you can easily store the result of the php function time() in a timestamp field in the database. Then for expiry, a cronjob for a php file which checks for expired items. This crojob would do: - $time = time(); - $limit = $time - (24*60*60); if 24 hours is chosen - then you query the database for entries where the timestamp field is less than $limit
'Y-m-d' is not a problem. An article expiring in one week would have the expiry time stamp: $expiry = date("Y-m-d", time() + 7*24*60*60 ); while an article expiring in one month might have the expiry time stamp: $expiry = date("Y-m-d", time() + 30*24*60*60 ); You would then store the expiry date. When the page is being built get today's date stamp: $today = date("Y-m-d", time()); Then do the comparison: if( $today > $expiry) { ## ignore expired article } else { ## display article } PHP: You could also extend legend2's idea and have a cron job go through the article database at 12:01 AM local time and mark expired articles as inactive. Then you would only display "active" articles.
// modify this setting your expiry time $expiry = date("Y-m-d", time() + 30*24*60*60 ); $today = date("Y-m-d", time()); if( $today > $expiry) { ## ignore expired article } else { ## display article } PHP: