1$dayofweek = date('w', strtotime($date));
2$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
1date('w'); //gets day of week as number(0=sunday,1=monday...,6=sat)
2
3//note:returns 0 through 6 but as string so to check if monday do this:
4if(date('w') == 1){
5 echo "its monday baby";
6}
1
2Things to be aware of when using week numbers with years.
3
4<?php
5echo date("YW", strtotime("2011-01-07")); // gives 201101
6echo date("YW", strtotime("2011-12-31")); // gives 201152
7echo date("YW", strtotime("2011-01-01")); // gives 201152 too
8?>
9
10BUT
11
12<?php
13echo date("oW", strtotime("2011-01-07")); // gives 201101
14echo date("oW", strtotime("2011-12-31")); // gives 201152
15echo date("oW", strtotime("2011-01-01")); // gives 201052 (Year is different than previous example)
16?>
17
18Reason:
19Y is year from the date
20o is ISO-8601 year number
21W is ISO-8601 week number of year
22
23Conclusion:
24if using 'W' for the week number use 'o' for the year.
25
1<?php
2 $week=29;
3 $year=2017;
4
5 function getStartAndEndDate($week, $year)
6 {
7 $dateTime = new DateTime();
8 $dateTime->setISODate($year, $week);
9 $result['start_date'] = $dateTime->format('d-M-Y');
10 $dateTime->modify('+6 days');
11 $result['end_date'] = $dateTime->format('d-M-Y');
12 return $result;
13 }
14
15 $dates=getStartAndEndDate($week,$year);
16 print_r($dates);
17
18?>
1$dateTime = new \DateTime();
2/**
3 * You can get the string by using format
4 */
5$dateTime->format('Y-m-d H:i:s');