If you have the year and the number of the week, and want a date corresponding to this week, just use DateTime::setISODate
:
$ano = 2019;
$semana = 32;
$d = new DateTime();
$d->setISODate($ano, $semana);
echo $d->format('d/m/Y');
In this case, it sets the date to the first day of the said week. How is the definition of the ISO 8601 (in which Monday is the first day of the week), the result will be 05/08/2019.
Optionally, you can get other days this week. For example, if you want the second day of this week (the Tuesday of the 32nd week of 2019, ie, 06/08/2019), just do:
$d->setISODate($ano, $semana, 2); // 06/08/2019
Just remembering that this way of working with "weeks of the year" can have some "strange" and counterintuitive results in the first and last week of the year.
For example, if we consider the end of 2018 and beginning of 2019:
Dom Seg Ter Qua Qui Sex Sab
23 24 25 26 27 28 29 <-- dez/2018
30 31 1 2 3 4 5 <-- dez/2018 e jan/2019
6 7 8 9 10 11 12 <-- jan/2019
If we want the Monday of the first week of 2019 ($d->setISODate(2019, 1, 1)
), the result will be 31/12/2018. Yes, according to ISO 8601 the first week of a year is the 7-day period beginning on a Monday and has at least 4 days that year. Thus, the first week of 2019 begins on 31/12/2018 and ends on 06/01/2019.
Heed: this "year" is called "week based year" (or simply week year), and is a field different from the "calendar year" (31/12/2018 is in the "calendar year" of 2018, but its week based year is 2019). Much attention, because the value of the year that setISODate
receives is the week based year (for most of the year, this value is the same as in the "calendar year", but for the end of December and beginning of January, there may be such differences).
To know the week based year of a date, use the format o
. See the difference for the "calendar year":
$d = new DateTime();
$d->setISODate(2019, 1, 1);
echo $d->format('d/m/Y o'); // 31/12/2018 2019
If you enter with the value of
N
, Thursday, why do you want to return the value ofN
, Monday?– Woss
because it has to take the date from today, the today is variable
– ThiagoKoller
I need it to return a date relative to the first day of week 32, so I can complete the table, do not necessarily use the N, I use it to reference the location where the $today
– ThiagoKoller
One detail: its variable
$hoje
contains a string with the date in the format 'dd/mm/yyyy'. But when the string is in this format,strtotime
interprets as "month/day/year" (read here for more details). Today is no problem because it is 08/08, but any other day. Anyway, if you want the current date, just dodate('W')
,date('N')', etc. (ou use
strtotime('now')`)– hkotsubo
dd/mm/yyyy is not day month and year?? read somewhere that date('N'... needs this format to return the value of the week
– ThiagoKoller
It is still not very clear. If you receive the year and the week (for example, week 32 of 2019), you want only the Monday for this week? Or do you want all seven dates that belong to this week? Ah, yes, you should also define what the definition of week will use (the first day of the week varies according to cultural, religious, etc - in some countries is Monday, in others is Sunday, etc - see more about this here)
– hkotsubo
According to the PHP documentation (https://www.php.net/manual/en/function.date.php), N is used the first day of the week as "1" for Monday, I only want the first day, then it automatically completes with a while, which is another story.. just need this "xx/08/2019" date that I will already be able to work with him to get the other days of the week
– ThiagoKoller
one more attempt to clarify.. Do you agree with me that week 32 is within month 8? so I wanted to know how to find this with good practices and not with gambiarra, the year I already return from $today, with this I can already mount xx/08/2019.. became clearer?
– ThiagoKoller
Ask the question that you want to calculate the number of weeks elapsed since a given date, because if it wasn’t for your last comment you would have given a vote to close the question.
– Augusto Vasques