last week, this week (php)
Asked Answered
D

5

7

i am making some statistics, i want to select the time from (last week only) and this week.

for this week its easy:

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

for last week

$start = strtotime('last week');
$finish = ??????
Downstream answered 19/4, 2011 at 0:19 Comment(1)
Do you realize that strtotime('this week') returns the same value as time() ?Twodimensional
D
16

This?

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

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

Depressed answered 19/4, 2011 at 0:21 Comment(2)
stupid me :D is it possible also to use -1 week for last week?Downstream
NOTE: This question and answer, do not match. If the question is about week, then the start of the week is monday and end is sunday. Depending in what region you live in of course. However, strtotime('1 week ago') will work actually on 7 days basis. There for this answer is for question, how to get 7 days ago, not for statistics program, that is supposed to show last week statistics!! Debug code: echo date("d.m.Y", strtotime("1 week ago", strtotime('yesterday')));Princeling
S
9

If the question is for statistical PHP script. Then all of the answers and basically the question 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 minus 7 days. That is not last or this week. So the selected answer currently, is in correct and counts 7 days back. Granted, its Sunday, at the time of testing, so it shows correct. But by editing the now date, you can see the 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 week and the end of the week. strtotime() can support more stuff, so you can most likely make this answer better and more neat. However you get the working code and a good example 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 00:00:00 - Today for example purposes
24.08.2015 00:00:00 - 30.08.2015 23:59:59 - Currently running week period
17.08.2015 00:00:00 - 23.08.2015 23:59:59 - Last week period

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

Sharp answered 30/8, 2015 at 14:50 Comment(1)
Now it's Monday, it's now taking the start of the current week back to last week's Monday.Delphina
B
2

Is that what you want?

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

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

Bosomed answered 19/4, 2011 at 0:20 Comment(0)
W
1

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

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

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

As for the months/years, I used the classy mktime 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'));
Woodenhead answered 8/3, 2016 at 12:1 Comment(0)
J
-1

If you are looking for "Last Week" instead of Last 7 days

$start = strtotime('Last Week'); // Will give you last Monday
$finish = strtotime('Last Sunday'); // Will give you last Sunday
Jobber answered 10/2, 2015 at 3:58 Comment(1)
You understand the problem, but your solution covers less then 24h, and not a week or 7 days. So its: $start = 'Sunday'; to $finish = 'Monday'; And they are not reversed they are: 24.08.2015 - 23.08.2015 for example.Princeling

© 2022 - 2024 — McMap. All rights reserved.