[ACCEPTED]-last week, this week (php)-strtotime

Accepted answer
Score: 15

This?

$start = strtotime('2 weeks ago');
$finish = strtotime('last week');

Edit: change credit to @Dominic Barnes's 1 comment.

Score: 8

If the question is for statistical PHP script. Then 24 all of the answers and basically the question 23 is wrong.

  • Last week in statistics = One before currently running week from Sunday to Monday
  • This week in statistics = Currently running week from Sunday to Monday

(or Monday to Sunday, depending on which calendar you are used to, but in PHP that's one week)

This means, its not from today 22 minus 7 days. That is not last or this week. So 21 the selected answer currently, is in correct 20 and counts 7 days back. Granted, its Sunday, at 19 the time of testing, so it shows correct. But 18 by editing the now date, you can see the 17 problem:

// Selected answer:
$start = strtotime('2 weeks ago');
$finish = strtotime('last week');

// But if today isn't Sunday, you can see the code is wrong:
echo date("d.m.Y", strtotime("1 week ago", strtotime('yesterday')));
// Output: 15.08.2015 00:00:00 - 17.08.2015 00:00:00

You have to set the start of the 16 week and the end of the week. strtotime() can support 15 more stuff, so you can most likely make 14 this answer better and more neat. However 13 you get the working code and a good example 12 of the logic from...

My proposed solution:

$today = strtotime('today 00:00:00');

$this_week_start = strtotime('-1 week monday 00:00:00');
$this_week_end = strtotime('sunday 23:59:59');

$last_week_start = strtotime('-2 week monday 00:00:00');
$last_week_end = strtotime('-1 week sunday 23:59:59');

echo date('d.m.Y H:i:s', $today) . ' - Today for example purposes<br />';
echo date('d.m.Y H:i:s', $this_week_start) . ' - ' . date('d.m.Y H:i:s', $this_week_end) . ' - Currently running week period<br />';
echo date('d.m.Y H:i:s', $last_week_start) . ' - ' . date('d.m.Y H:i:s', $last_week_end) . ' - Last week period<br />';

Above currently produces:

30.08.2015 11 00:00:00 - Today for example purposes
24.08.2015 10 00:00:00 - 30.08.2015 23:59:59 - Currently 9 running week period
17.08.2015 00:00:00 8 - 23.08.2015 23:59:59 - Last week period

Because 7 for statistics, it has to be accurate and 6 if the end would be 00:00:00, then that 5 date wont be counted in. And if the date 4 would be one day later at 00:00:00, then 3 the date is not correct. There for, this 2 solution is the correct way to do this, for 1 statistical purposes at least.

Score: 2

Is that what you want?

$start = strtotime('last week');
$finish = strtotime('this week');

Dominic also points out that 1 time() === strtotime('this week') (CodePad).

Score: 1

If searching for the last week for statistical 7 purposes, starting on Monday, ending on 6 Sunday:

$last_week_start = strtotime("monday last week");
$last_week_end   = strtotime("monday this week - 1 second");

"this week" is important, otherwise, if 5 this weeks monday is already in the past 4 (e.g. if it is tuesday already), it will 3 give you the monday of next' week.

As for 2 the months/years, I used the classy mktime 1 approach:

last month

$last_month_start = mktime(0, 0, 0, date('m')-1, 01);
$last_month_end   = mktime(23, 59, 59, date('m'), 0);

last year

$last_year_start = mktime(0, 0, 0, 1, 1, date('Y')-1);
$last_year_end   = mktime(23, 59, 59, 1, 0, date('Y'));

More Related questions