Say you want to get a set of timestamps so that you can query a database for all records that fall between the start of this week, and the end of this week.
One would logically try something like:
It turns out trying to find relative time references with strtotime is wonky. Depending on which day it currently is, and what day you're trying to use as a frame of reference, you will get different results when using 'this monday' or 'tuesday next week' etc... The results are unintuitive, but more importantly, inconsistent in most cases.
Since strtotime() lets us do lazy math to get timestamps (e.g. 'this monday - 1 week'), all we need to do is find some date string that works consistently 7 days of the week, and then we can reliably find other timestamps relative to that string. Any date string that does not have the same results for all 7 days, cannot be used as a frame of reference, as its value will change depending on what day it is!
From the tables below, we can see that the following three strings can be safely used as frames of reference in strtotime():
My suspicion is that certain strings consider Sunday to be the start of the week, while others consider Monday to be the start of the week, and probably round the time up or down in different ways.
Note that I have arbitrarily chosen Monday - March 4th, 2013 through Sunday - March 10th, 2013 as the week to test. Though given how wonky strtotime()'s behavior is, there's a possibility that the behavior shown below varies from one week to the next! However, I did test various times throughout each day (e.g. 00:00:00 and 23:59:59) and thankfully the results were consistent.
So going back to our problem of getting timestamps for the start and end of this week, the cleanest and most reliable solution is this:
If today = | Mon, March 4th | Tue, March 5th | Wed, March 6th | Thu, March 7th | Fri, March 8th | Sat, March 9th | Sun, March 10th |
strtotime("this monday") = | March 4th | March 11th | March 11th | March 11th | March 11th | March 11th | March 11th |
strtotime("monday this week") = | March 4th | March 4th | March 4th | March 4th | March 4th | March 4th | March 4th |
strtotime("next monday") = | March 11th | March 11th | March 11th | March 11th | March 11th | March 11th | March 11th |
strtotime("monday next week") = | March 11th | March 11th | March 11th | March 11th | March 11th | March 11th | March 11th |
strtotime("last monday") = | February 25th | March 4th | March 4th | March 4th | March 4th | March 4th | March 4th |
strtotime("monday last week") = | February 25th | February 25th | February 25th | February 25th | February 25th | February 25th | February 25th |
If today = | Mon, March 4th | Tue, March 5th | Wed, March 6th | Thu, March 7th | Fri, March 8th | Sat, March 9th | Sun, March 10th |
strtotime("this tuesday") = | March 5th | March 5th | March 12th | March 12th | March 12th | March 12th | March 12th |
strtotime("tuesday this week") = | March 5th | March 5th | March 5th | March 5th | March 5th | March 5th | March 5th |
strtotime("next tuesday") = | March 5th | March 12th | March 12th | March 12th | March 12th | March 12th | March 12th |
strtotime("tuesday next week") = | March 12th | March 12th | March 12th | March 12th | March 12th | March 12th | March 12th |
strtotime("last tuesday") = | February 26th | February 26th | March 5th | March 5th | March 5th | March 5th | March 5th |
strtotime("tuesday last week") = | February 26th | February 26th | February 26th | February 26th | February 26th | February 26th | February 26th |
If today = | Mon, March 4th | Tue, March 5th | Wed, March 6th | Thu, March 7th | Fri, March 8th | Sat, March 9th | Sun, March 10th |
strtotime("this wednesday") = | March 6th | March 6th | March 6th | March 13th | March 13th | March 13th | March 13th |
strtotime("wednesday this week") = | March 6th | March 6th | March 6th | March 6th | March 6th | March 6th | March 6th |
strtotime("next wednesday") = | March 6th | March 6th | March 13th | March 13th | March 13th | March 13th | March 13th |
strtotime("wednesday next week") = | March 13th | March 13th | March 13th | March 13th | March 13th | March 13th | March 13th |
strtotime("last wednesday") = | February 27th | February 27th | February 27th | March 6th | March 6th | March 6th | March 6th |
strtotime("wednesday last week") = | February 27th | February 27th | February 27th | February 27th | February 27th | February 27th | February 27th |
If today = | Mon, March 4th | Tue, March 5th | Wed, March 6th | Thu, March 7th | Fri, March 8th | Sat, March 9th | Sun, March 10th |
strtotime("this thursday") = | March 7th | March 7th | March 7th | March 7th | March 14th | March 14th | March 14th |
strtotime("thursday this week") = | March 7th | March 7th | March 7th | March 7th | March 7th | March 7th | March 7th |
strtotime("next thursday") = | March 7th | March 7th | March 7th | March 14th | March 14th | March 14th | March 14th |
strtotime("thursday next week") = | March 14th | March 14th | March 14th | March 14th | March 14th | March 14th | March 14th |
strtotime("last thursday") = | February 28th | February 28th | February 28th | February 28th | March 7th | March 7th | March 7th |
strtotime("thursday last week") = | February 28th | February 28th | February 28th | February 28th | February 28th | February 28th | February 28th |
If today = | Mon, March 4th | Tue, March 5th | Wed, March 6th | Thu, March 7th | Fri, March 8th | Sat, March 9th | Sun, March 10th |
strtotime("this friday") = | March 8th | March 8th | March 8th | March 8th | March 8th | March 15th | March 15th |
strtotime("friday this week") = | March 8th | March 8th | March 8th | March 8th | March 8th | March 8th | March 8th |
strtotime("next friday") = | March 8th | March 8th | March 8th | March 8th | March 15th | March 15th | March 15th |
strtotime("friday next week") = | March 15th | March 15th | March 15th | March 15th | March 15th | March 15th | March 15th |
strtotime("last friday") = | March 1st | March 1st | March 1st | March 1st | March 1st | March 8th | March 8th |
strtotime("friday last week") = | March 1st | March 1st | March 1st | March 1st | March 1st | March 1st | March 1st |
If today = | Mon, March 4th | Tue, March 5th | Wed, March 6th | Thu, March 7th | Fri, March 8th | Sat, March 9th | Sun, March 10th |
strtotime("this saturday") = | March 9th | March 9th | March 9th | March 9th | March 9th | March 9th | March 16th |
strtotime("saturday this week") = | March 9th | March 9th | March 9th | March 9th | March 9th | March 9th | March 9th |
strtotime("next saturday") = | March 9th | March 9th | March 9th | March 9th | March 9th | March 16th | March 16th |
strtotime("saturday next week") = | March 16th | March 16th | March 16th | March 16th | March 16th | March 16th | March 16th |
strtotime("last saturday") = | March 2nd | March 2nd | March 2nd | March 2nd | March 2nd | March 2nd | March 9th |
strtotime("saturday last week") = | March 2nd | March 2nd | March 2nd | March 2nd | March 2nd | March 2nd | March 2nd |
If today = | Mon, March 4th | Tue, March 5th | Wed, March 6th | Thu, March 7th | Fri, March 8th | Sat, March 9th | Sun, March 10th |
strtotime("this sunday") = | March 10th | March 10th | March 10th | March 10th | March 10th | March 10th | March 10th |
strtotime("sunday this week") = | March 10th | March 10th | March 10th | March 10th | March 10th | March 10th | March 10th |
strtotime("next sunday") = | March 10th | March 10th | March 10th | March 10th | March 10th | March 10th | March 17th |
strtotime("sunday next week") = | March 17th | March 17th | March 17th | March 17th | March 17th | March 17th | March 17th |
strtotime("last sunday") = | March 3rd | March 3rd | March 3rd | March 3rd | March 3rd | March 3rd | March 3rd |
strtotime("sunday last week") = | March 3rd | March 3rd | March 3rd | March 3rd | March 3rd | March 3rd | March 3rd |