Last week, this week (php)

I am doing some statistics, I want to choose the time (only last week) and this week.

this week is easy:

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

for the last week

$start = strtotime('last week');
$finish = ??????
+5
source share
5 answers

It?

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

Edit: change credit to comment @Dominic Barnes.

+11
source

If the question is for a statistical PHP script. Then all the answers and basically the question is wrong.

  • Last week in statistics = one until the current current week from Sunday to Monday
  • This week in statistics = Current week from Sunday to Monday

( , , , PHP )

, 7 . . , 7 . , , , . , , :

// 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

. strtotime() , , , . ...

:

$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 />';

:

30.08.2015 00:00:00 - , ,
24.08.2015 00:00:00 - 30.08.2015 23:59:59 - 17.08.2015 00:00:00 - 23.08.2015 23:59:59 -

, 00:00:00, . 00:00:00, . , , , , .

+5

, ?

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

Dominic , time() === strtotime('this week') (CodePad).

+2

, , :

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

" " , , (, ), .

/, mktime:

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

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

" " 7

$start = strtotime('Last Week'); // Will give you last Monday
$finish = strtotime('Last Sunday'); // Will give you last Sunday
-1

All Articles