简体   繁体   中英

PHP: how to get a day in last week?

Is there an easy way to get last week's, say, Monday? If today is Tuesday, I do not want yesterday's Monday. Rather, I want the Monday 8 days ago (last week's Monday). Then I want that Monday's proceeding Sunday. Basically, I'm trying to get the date range for last week, Monday to Sunday.

This doesn't always work right:

date('Ym-d', strtotime('last Monday')

Suggestions?

You can use “this week” format:

$monday = strtotime( 'this week', strtotime( '7 days ago' ) );
$sunday = strtotime( '+ 6 days', $monday  );

eval.in demo

“this_week” returns monday of previous week, then — adding 6 days — you obtain the monday of relative week.

The strtotime function accepts the current date as a parameter.

http://php.net/manual/en/function.strtotime.php

Just pass in strtotime('last Sunday') as the parameter to get a weekday of the last full week.

$beginning_of_week = strtotime('last Sunday');
$result = date('Y-m-d', strtotime('last Monday', $beginning_of_week));

echo $result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM