
strtotime() is a function that, while widely discussed, is generally misunderstood. The application of strtotime() is far reaching. You can use it to get yesterday’s date (even when today turns into tomorrow) or the date of next year (even when this year rolls over).
strtotime() needs a phrase or string to convert into a UNIX timestamp. Usually, you’ll input something like -7 days, +20 days or 02 May 2010. When you put in a string for a date, be aware that it must be in day-month-year format.
Easy example: you want to display yesterday’s date.
$yesterday = strtotime("-1 day");
echo("Yesterday was ".date("F d, Y", $yesterday));
// Yesterday was February 28, 2010
How about something harder? Let’s say you have a blog and want to have a page just for the posts from the last seven days. You’ll need to format your dates a bit for WordPress to understand the request, using the date() function, but it’s easy enough.
$lastWeek = date("Y-m-d", strtotime("-7 days"));
$today = date("Y-m-d");
This section sets the time boundaries. We want the (WordPress formatted) date for 7 days ago and the (WordPress formatted) date for today. The WordPress database formats dates as YYYY-MM-DD hh:mm:ss. Since we aren’t concerned about the hours, minutes or seconds, we’ll just format it for the earliest time one week ago, or 2010-02-21.
Now that we have the date, we can use the custom select query to select only the posts that fall in that date range. The WordPress Codex has a good primer on how to display posts using a custom select statement. I’m basing the below query on that.
$querystr = "SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wposts.post_date >= '$lastWeek'
AND wposts.post_date <= '$today'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC;";
$posts = $wpdb->get_results($querystr, OBJECT);
In the query, we’re telling WordPress to select the post and post meta data from the appropriate tables, to make sure the IDs of the entries match, and to limit the search based on date and post_type. wposts.post_date is the limiter for date. WordPress will look for any post that occurred between 7 days ago and today.
Now that we have the data, we can pass it into The Loop, which is the WordPressy way to say we’re going to display the posts.
get_header(); ?>
<div id=”content” role=”main”>
<?php
if($posts) {
foreach($posts AS $post) {
setup_postdata($post);
?>
Your template’s HTML goes here
Your template came with the Loop by default, in the form of if (have_posts()) : while (have_posts()) : the_post();. You’ll need to remove these lines. In the end, your front page should look similar to those, using the default Kubrick theme.
<?php
/**
* @package WordPress
* @subpackage Default_Theme
*/
$lastWeek = strtotime(“-7 day”);
$lastweek = date(“Y-m-d”, $lastWeek);
$today = date(“Y-m-d”);
$querystr = ”
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wposts.post_date > ‘$lastWeek’
AND wposts.post_date < ‘$today’
ORDER BY wposts.post_date DESC
“;
$posts = $wpdb->get_results($querystr, OBJECT);
get_header(); ?>
<div id=”content” role=”main”>
<?php
if($posts) {
foreach($posts AS $post) {
setup_postdata($post);
?>
<div <?php post_class() ?> id=”post-<?php the_ID(); ?>”>
<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h2>
<small><?php the_time(‘F jS, Y’) ?> by <?php the_author() ?></small>
<div>
<?php the_content(‘Read the rest of this entry »’); ?>
</div>
<p><?php the_tags(‘Tags: ‘, ‘, ‘, ‘<br />’); ?> Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments »’, ’1 Comment »’, ‘% Comments »’); ?></p>
</div>
<?php } ?>
<div>
<div><?php next_posts_link(‘« Older Entries’) ?></div>
<div><?php previous_posts_link(‘Newer Entries »’) ?></div>
</div>
<?php } else { ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn’t here.</p>
<?php get_search_form(); ?>
<?php } ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
If you have any questions on or improvements for this code, let me know. And, as always, enjoy!
Feedback