Filtering by Time Period5:14 with Alena Holligan
The last filter we're going to create is a filter by date. To keep all the reports in a simple dropdown, we're going to create some predefined ranges: last week, this week, last month, and this month.
date() returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
strtotime() function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC).
Date and Time Usage
Dates and times can start to get interesting when the server that the website is running on is set to a date/time other than your local date/time. When working internationally, it is especially important to consider dates. PHP includes many Date/Time functions and it's a good idea to familiarize yourself with the options. One way to manage dates is to have the server and database store dates in a standard such as UTC and then using local time to display those date.
PHP also offer more features using the Object-Oriented method of the DateTime class.
The last filter I wanna create is a filter by date. 0:00 Oftentimes, you'll see date reports that allow you to enter a range. 0:04 This can give your report a lot of flexibility and 0:08 allow users to customize what they see. 0:11 For this application, I want all my reports to be in a simple drop-down, 0:14 so I'm going to create some predefined ranges. 0:18 Last week, this week, last month, and this month. 0:22 Let's start by adding our date ranges to the drop-down. 0:26 In reports.php, we're going to add another optgroup for date. 0:29 Now, we can start adding our date options. 0:40 We use date as our item, and then separated it with a colon. 0:45 Since these will be date ranges and not a single date, 0:51 we'll separate our starting and ending dates with a colon is well. 0:55 We'll use PHP to determine those dates. 0:59 Each date will be in the format month, day, year. 1:02 We can calculate the dates using the strtotime function. 1:16 Our first option would be last week. 1:21 To calculate the start date of last week, we use strtotime('-2 Sunday'). 1:25 Then we had a colon separator. 1:34 Echo our date as well, And add our ending date. 1:42 This time, for strtotime, We use ('-1 Saturday). 1:52 Now we finish the option, naming it Last Week. 2:04 Let's duplicate this option three more times. 2:11 Naming them This Week, 2:23 Last Month, And This Month. 2:28 For this week, Our start date, we'll use ('-1 Sunday'). 2:35 And the end date will be the current date, so we can remove the strtotime completely. 2:42 For last month, we'll use this string, 2:50 ('first day of last month'). 2:57 And for the end date, we'll use 'last day of last month'. 3:01 And finally, for this month, we'll start with, 'first day 3:09 of this month', and we'll end with today. 3:15 Filtering the input still works because we're creating 3:25 an array using colon as a separator. 3:29 This means that the date filter will have three elements instead of just two. 3:33 We need to update the get_task_list function 3:39 once again to utilize the date range. 3:42 We add another case in our switch statement. 3:46 And this time, $where = 'WHERE >= our placeholder AND 3:56 date <= our second placeholder. 4:05 Then we need to add an additional bind value for our second placeholder. 4:13 if ($filter == 4:17 'date') $results->bindValue, 4:23 our second placeholder, 4:31 $filter. 4:37 And for this one, we can use PDO::PARAM_STR. 4:41 Let's preview our report in the browser again. 4:48 Looks like we messed up our function file a little bit. 4:51 Let's close this if. 4:56 Now, let's try that again. 4:59 We see the date options in the Filter drop-down, and 5:06 when we select a date range, the results are filtered by this date range. 5:09
You need to sign up for Treehouse in order to download course files.Sign up