简体   繁体   中英

Getting dates for week number last year, php

I'm using a date function to simply get the week number for the current week

$weekNumber = date("W");

THis works fine but I'm having a hard time figuring out how to use a php date function to take this week number, look at the corresponding week for that number of last year and get both that monday and friday.

So say it's week number 50, I want to look at week 50 of last year and get the date for Monday and Friday of that week

I know how to get this current date of last year with $todayLastYear = new \DateTime(date('Ym-d', strtotime('last year')));

But I can't seem to find anything where I can get the monday and friday of this week number for last year

Does anyone have any experience getting this with a date() function?

You could use setISODate for that:

$date = new DateTime();
$date->setISODate(date("Y")-1, date("W"));

$monday = $date->format('Y-m-d');
$friday = $date->modify('+4 days')->format('Y-m-d');

print($monday);
print($friday);
$end_week = ( date('D Y-m-d', strtotime('Dec 31'))); // this will get the last date of current year
echo $end_week.'<br>';
for($i=1;$i<=7;$i++){
$end_week2 = date("D Y-m-d",strtotime($end_week.' -'.$i.' day'));
echo $end_week2.'<br>';
}


result :
Thu 2020-12-31
Wed 2020-12-30
Tue 2020-12-29
Mon 2020-12-28
Sun 2020-12-27
Sat 2020-12-26
Fri 2020-12-25
Thu 2020-12-24

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