7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.3 Retrieving Past and Upcoming Episodes

Now that we have a list of episodes, let's look at how we can use that episode data to retrieve only the past or future episodes. In this lesson, I'll show you how to iterate over the instantiated list of Episode objects and filter them by publish date. Let's step through it and see how it's done.

3.3 Retrieving Past and Upcoming Episodes

Now that we have a list of episodes. Let's look at how to use that episode data to retrieve only the past episodes. Put simply, a past episode is any episode which has a publish date no later than today. Or actually, more specifically, no later than right now. We're going to iterate over the instantiated list of episode objects. And filter out any which have a publish date somewhere in the future. Let's step through it and see how it's done. We first create a new class, called PastEpisodeFilterIterator. Located inside src/PodcastSite/Iterator. As with the previous class, the constructor will receive an iterator, called iterator. Which itself contains a list of instantiated episode objects. This is done, by passing it to the parent constructor. Then calling the rewind method, to insure that we start at the start of a data set. Now let's define the accept method. To filter out future episodes, we'll first retrieve the current item. By calling the current method, on the result of calling the getInnerIterator method. Then we'll instantiate a new DateTime object, called episode. Passing nothing to the constructor. This initializes it using the current system timestamp. Then we'll instantiate a second DateTime object, called today. Using the episode's published date value. Obtained by calling the getPublishDate method on it. Finally, we'll return the result of checking, if episode is earlier than today. If so, it will return true, making the record valid. And if not, it will return false. Making the record invalid, and skipping over it. I took this comparison approach, because you can compare two daytime objects. As you would compare strings, insigns, and so on. It makes it really easy to find out which one has an earlier or later time stamp. And this is a handy, easy form of date comparison. It saves us a lot of effort in coding a date time comparison routine. Now let's look at retrieving upcoming episodes. I want to spend a lot of time on this. As the upcoming episodes accept method. Is almost exactly the same as that of the past episodes. Given that, I've pre-written a class. Which you can find in src/PodcastSite/Iterator/UpcomingEpisodeF- ilterIterator.php. I've cut straight down to the return comparison. Just so that you know I'm not cheating you out of anything. And that's it for this video. In the next video, we'll start tying the code we've written together, and initialize it.

Back to the top