Pick up the start day and the end day of the specified week

Asked

Viewed 50 times

0

Good afternoon, I’m returning from the database the number of the week in the year.

["data_hora"]=>
string(16) "19/11/2018 15:26"
["num_semana"]=>
string(2) "47"

Where num_week is the number of the week of the year that the date 19/11/2018 this inserted. I need a way to take the first day of week 47 (Sunday) and the last day (Saturday). Does anyone have any idea how I can do this.

  • Something like new DateTime('+46 weeks Sunday Jan 2018')

  • More or less, but I need to pass the value of num_week, and it tells me when it starts and when it ends. Example num_week = 1 $inicio = 01/01/2018 $fim = 07/01/2018

  • Exactly. Just a matter of replacing the value.

  • new DateTime('+46 weeks Sunday Jan 2018') would not return me only the end date ?

  • 19 of 11 is in 47?

  • @Leocaracciolo Yes the week 47 starts on the day 19/11 and goes up to 25/11

  • https://ideone.com/Rvg4pb

  • @Leocaracciolo This return is the last day of the week 46 ?

  • 1

    incompatibility, in question Sunday is the first day and in comment the week begins on Monday

  • take the first day of week 47 (Sunday) and the last day (Saturday). and in the commentary the week 47 starts on 19/11 and goes until 25/11 day 19 is Monday

  • @Leocaracciolo Truth, I made this mistake at the time of the question, but anyway. I think this method that you went through will solve my problem.

  • Remembering that the definition of the first day of the week varies according to place, culture, religion, etc and depending can be Sunday, Monday or even Saturday: https://www.quora.com/What-is-the-first-day-of-the-week. If I’m not mistaken, in PHP setISODate uses the definition of ISO 8601, which uses Monday as the first day of the year, and to define the first week of the year, considers that it is the week that starts on a Monday and has at least 4 days in 2018: https://en.wikipedia.org/wiki/ISO_week_date#First_week

  • http://sandbox.onlinephpfunctions.com/code/4dedf06de8252117df8837c4895864aae0a375e3

  • In that case the last day would be https://ideone.com/t1Kk3y

Show 9 more comments

1 answer

1


To get last day date (Sunday) ideone

date_default_timezone_set('Europe/Amsterdam');
$date = new DateTime();

$date->setISODate(2018, 47, 7);

echo $date->format('Y-m-d');

and for the first day remove the , 7 that is to say, $date->setISODate(2018, 47);

Bonus - returning an array ideone

 function pegarInicioFimSemana($semana, $ano) { $dto = new DateTime(); $dto->setISODate($ano, $semana); $ret['primeiro_dia'] = $dto->format('d-m-Y'); $dto->modify('+6 days'); $ret['ultimo_dia'] = $dto->format('d-m-Y'); return $ret; } $semana_array = pegarInicioFimSemana(47,2018); print_r($semana_array);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.