jbrill Posted May 8, 2007 Share Posted May 8, 2007 Hey Everyone, Im new to this board and to PHP. Im looking for help with a certain peice of of code that is crucial to the current site im working on. I have created a customized news release where the customer can log in,enter a title and content, which then posts it in a stylized manner on the main website. However he recently requested that there be a "Release on: [date]" and "Expire on: [date]" Basically, he wants to be able to preload the database with news releases that are set to realease on a certain date, and then expire on another date. how would i go about doing this? I would really appreciate some help on this Thanks Alot! Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/ Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Add the 2 fields to the database and code a sql statement that states if the release date is not greater than equal to current and the expire date has not passed yet, display this news release. Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248430 Share on other sites More sharing options...
jbrill Posted May 8, 2007 Author Share Posted May 8, 2007 Do you think you could write that in code so I can visually see what you mean? As I said, im pretty new to this PHP thing I use the variable $today for today's date it displays from this code: <?php $today = date("F j, Y "); // May 8, 2007 ?> Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248435 Share on other sites More sharing options...
obsidian Posted May 8, 2007 Share Posted May 8, 2007 Do you think you could write that in code so I can visually see what you mean? You will want to use the DATE or DATETIME field in the MySQL database to store your date. Basically, this means that you need to format your date into a YYYY-MM-DD format for insert. Once you have the dates in the database, you can simply query for all records BETWEEN those two dates: SELECT * FROM myTable WHERE CURDATE() BETWEEN startDate AND endDate; Hope this helps point you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248441 Share on other sites More sharing options...
jbrill Posted May 8, 2007 Author Share Posted May 8, 2007 Awesome! thanks for the hlp guys. Let me give this a try.... Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248444 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 <?php $releaseDate = strtotime("05/01/2007"); // should be retireved from teh database in some format $expireDate = strtotime("05/08/2007"); // should be retireved from teh database in some format // strtotime is used to convert the above to unix timestamps for comparison. $today = time(); // unix timestamp of today if ($releaseDate >= $today && $expireDate < $today) { print "The article should be printed here"; }else { print "The article has expired."; } // The above is the PHP version, the mysql version would be something like this. // assuming the date is stored as an INT(11) in the database with // column names expiredate and releasedate that hold unix timestamps: $getArticles = "SELECT * FROM table_articles WHERE releasedate >= " . $today . " AND expiredate < " . $today . " ORDER BY releasedate;"; $res = mysql_query($getArticles) or die(mysql_error()); while ($row = mysql_fetch_array($res)) { print $row['articlename']; // print article data here } ?> The mysql portion I am not sure if it allows the >=, but yea. That is "an example" of what the code would look like. Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248454 Share on other sites More sharing options...
jbrill Posted May 8, 2007 Author Share Posted May 8, 2007 Do you think you could write that in code so I can visually see what you mean? You will want to use the DATE or DATETIME field in the MySQL database to store your date. Basically, this means that you need to format your date into a YYYY-MM-DD format for insert. Once you have the dates in the database, you can simply query for all records BETWEEN those two dates: SELECT * FROM myTable WHERE CURDATE() BETWEEN startDate AND endDate; Hope this helps point you in the right direction. I used this technique and it is working perfectly, expect for one thing... If I only enter a start date, and leave the expiry date blank ( so it will never expire) it will not display the nes because it defaults to 0000-00-00 how can i get around this? Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248486 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 make it default to 2037-12-31 I think that is the highest date you can use and hopefully it would have been deleted before than. Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248499 Share on other sites More sharing options...
obsidian Posted May 8, 2007 Share Posted May 8, 2007 I used this technique and it is working perfectly, expect for one thing... If I only enter a start date, and leave the expiry date blank ( so it will never expire) it will not display the nes because it defaults to 0000-00-00 how can i get around this? I would use some sort of CASE statement in my query to test whether or not there is an endDate, and if not, just return tomorrow's date so it never expires: SELECT * FROM myTable WHERE CURDATE() BETWEEN startDate AND (CASE WHEN endDate = '000-00-00' THEN CURDATE + INTERVAL 1 DAY ELSE endDate END CASE) Quote Link to comment https://forums.phpfreaks.com/topic/50554-news-releasedate-help/#findComment-248515 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.