ZeroTek
ZeroTek

Reputation: 1312

php: Calculate Date from Week of Year and Day of Week

I am currently struggling with the following: I need to get the Date like "Sun, 29.05.2016 18:00" but all i have is the number of the week from the beginning of the year, the number of the day of week and the hour of the day.

Week: 21
Day:  7
Hour: 18

Now my question is how do i get the actual date and time with php's date functions or own code?

Upvotes: 3

Views: 183

Answers (3)

Vishwa
Vishwa

Reputation: 1545

You are looking for DateTime::setISODate

$week_no = 21;
$day =7;
$hour = 18;

$d= new DateTime();
$d->setISODate(date('Y'),$week_no, $day);
$d->setTime ($hour,0);
echo $d->format('D, d.m.Y H:i');

Upvotes: 3

Murad Hasan
Murad Hasan

Reputation: 9583

Try with setISODate and DateTime. Online Check

Setting the 2016 from your desire output and use the hour directly to the format.

$gendate = new DateTime();
$gendate->setISODate(2016, 21, 7); //year , week num , day
echo $gendate->format('D, d.m.Y 18:00'); //Sun, 29.05.2016 18:00

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212522

Pretty straightforward with DateTime objects:

$week = 21;
$day = 7;
$hour = 18;

$dateFormat = sprintf('%d-W%02d-%d', (new DateTime())->format('Y'), $week, $day);

$x = new DateTime($dateFormat);
$x->setTime($hour, 0);
echo $x->format('Y-m-d H:i:s');

(assuming the current year)

Upvotes: 1

Related Questions