Ok so I want to get the Monday of week 16 in 2010 (more precisely to week $week of year $year). here is what I have in my test.php file
$week = 16;
$year = 2010;
$dateString = $dateString = 'Start of week ' . $week . ' in ' . $year;
$date = strtotime( $dateString );
echo $date;
The value returned is false. I have also tried using Monday of week... but this also gave a false result.
any idea how to get a date given a week number and the year?
thanks
CodePudding user response:
I'd recommend using the builtin \DateTime
class.
Inspired by the answer here I tested the following code:
<?php
function getStartDate($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
return $dto->format('Y-m-d');
}
$week = 16;
$year = 2010;
var_dump(getStartDate($week, $year));
This returned the following:
/some/fancy/path/test.php:12:string '2010-04-19' (length=10)
CodePudding user response:
Monday is the first day of the week. Make use of setISODate()
<?php
$gendate = new DateTime();
$gendate->setISODate(2022,2,1); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints" 10-01-2022