Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP CRUD Operations with PHP Reading and Writing Reports Filtering by Time Period

Youssef Zaki
Youssef Zaki
7,768 Points

PHP Why the year doesn't match ?

I have written the code exactly the same everything seems to work but the year of the tasks doesn't match today's year and tasks still appear at 2016

https://teamtreehouse.com/library/filtering-by-time-period

3 Answers

The criteria doesn't filter the year with what was written in the program. You would need to get more specific with your strtotime() function in order to get what you're looking for. The "Last Week" part in the program we write here literally means (At the time or writing this) From "07/27/xxxx" TO "08/03/xxxx", which is weird... but specificity counts!

http://php.net/manual/en/datetime.formats.relative.php

That helps...a little bit. I would try to add another phrase to the string.

It's possible that you forgot an echo statement. I know I did when I looked up your question for an answer. Here is a code snippet for last week.

<option value="date:<?php
//I don't think she added the "echo" to the next line you see in this code
echo date('m/d/Y',strtotime('-2 Sunday'));
//Which effectively caused there to be no start date.  Make sure to add echo
echo ":";
echo date('m/d/Y',strtotime('-1 Saturday'));
?>">Last Week </option>

A key point to mention here is that you CAN concatenate all of your echo statements into one like this if it suits you.

<option value="date:<?php
echo date('m/d/Y',strtotime('-2 Sunday')) . ":" . date('m/d/Y',strtotime('-1 Saturday'));
?>">Last Week </option>

The start time and end time must BOTH be echoed ALONG WITH the colon separator in the middle to ensure that you are, indeed, getting the beginning time as well as the end. Try this fix and see if you get the results you are looking for.

If that didn't fix your problem, let me know. Make sure to post the code that you are stuck with as well if you decide that what I talked about isn't enough.

Youssef Zaki
Youssef Zaki
7,768 Points

Yes I did exactly the same as your code and date appears right . The main problem is that the year's tasks doesn't match with the result I have chosen from the drop down last week , task1 Date : 07/29/2016 and task2 Date 07/25/2016 , this is last week : yes correct but a year ago, still appear why should they appear if the year is different ?.

Ryan Dainton
Ryan Dainton
17,164 Points

Hi Youssef

I had the same issue working on my own dev environment (not Workspace). This likely isn't a problem with your php, but with how your database is set up. I solved it by changing the datatype of the 'date' column in the .sql file from undefined (ie 'text') to 'Date' as I thought this might be why dates from earlier years were being included. I had to change the date format in all my php to 'yyyy/mm/dd' to match the format of the 'Date' datatype in mySQL but this solved the problem.

Hope this helps, Ryan