I have a day of the week number 'N' (1 for Monday through 7 Saturday) and a week 'W' and a year 'Y'.
How can i find the date?
Day of the week: 4 Week: 35 Year: 2014
Should output: 2014-08-28 (Thursday)
I have tried using strotime() without any luck. I cannot seem to find a way to specify the week and year, together with the weekday number.
You can use DateTime
's setISODate()
method for exactly this purpose. Your solution is almost right, but I don't understand why you're not supplying the weekday argument right from the start.
function get_date($weekday, $week, $year) {
return (new DateTime())->setISODate($year, $week, $weekday)->format('Y-m-d');
}
var_dump(get_date(4, 35, 2014));
Just did my own solution. Here is how you could do it.
function getDayDate($day, $week, $year) {
$day -= 1;
$dto = new DateTime();
$dto->setISODate($year, $week);
$dto->format('Y-m-d') . ' 00:00:00'; // start of the week date
$dto->modify('+'.$day.' days'); // add the days
$result = $dto->format('Y-m-d') . ' 23:59:59';
return $result;
}
$getdate = getDayDate(4, 35, 2014);
echo $getdate;