Add Delay Feature to Newsfeed
The newsfeed feature I developed a few years back works as expected. At times, however, I wish it would let me have a post automatically appear after a specific time. Especially now that I'm used to services like HootSuite that allow me to schedule tweets and other social media postings.
Background
The newsfeed is currently set to display the first six news entries on a website's home page in reverse chronological order. The entries come from a database table with the following columns:
- title
- description
- link
- date
To get the entries for the home page, I use the following query:
$sql = "SELECT title, description, link,
DATE_FORMAT(date, '%b %e') AS displayDate
FROM newsfeedTable
ORDER BY date DESC
LIMIT 7";
Note that the query is set to grab more than six entries. The seventh one is just to test whether there are more entries to display. If there are, the newsfeed provides a link to view more.
Delay Feature
To add the ability to delay posts, we will utilize MySQL's NOW() function to get the news entries that are less than or equal to the current date and time.
$sql = "SELECT title, description, link,
DATE_FORMAT(date, '%b %e') AS displayDate
FROM newsfeedTable
WHERE date <= NOW()
ORDER BY date DESC
LIMIT 7";
Final Thoughts
When using the NOW() function, keep in mind that the date column used in the comparison needs to be set to "datetime". That way the dates are formatted as "YYYY-MM-DD HH:MM:SS".
0 Comments
There are currently no comments.
Leave a Comment