How many weeks are in a year? If you ask around, most people will tell 52
,
but the right answer would be: 52
or 53
, depending on the year. To be
precise, an average year is exactly 52.1775
weeks long, and that excess
accumulates until a year is given an extra week.
For example, the most recent year with 53 weeks was 2015. December 31st 2015 is
considered part of the Week 53 of 2015 (formally, 2015-W53
), and January 1st
2016 is also considered part of the Week 53 of 2015. In contrast, both December
31st 2019 and January 1st 2020 are considered part of the Week 1 of 2020
(formally, 2020-W01
).
All this is defined in the ISO 8601 standard and it's one of the many confusing behaviors you'll find when working with dates. This is also something that Symfony should solve for you so you can focus on more important things.
In Symfony 4.4, we've added a new WeekType
form field that allows users to
modify data that represents a specific ISO 8601 week number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use Symfony\Component\Form\Extension\Core\Type\WeekType;
$builder->add('startDateTime', WeekType::class, [
// use this if you store week numbers as strings ('2011-W17')
'input' => 'string',
// use this if you store week numbers as arrays (e.g. [2011, 17])
'input' => 'array',
// renders two <select> to select the year and week number
'widget' => 'choice',
// renders two <input type="text"> to write the year and week number
'widget' => 'text',
// renders a <input type="week"> which is properly rendered by most browsers
'widget' => 'single_text',
]);
If your target browsers support it, set the widget
option to
single_text
to render the form field as <input type="week">
, which
provides a better user experience when dealing with this kind of data.
Thanks :) Is there a form validator for this also? (especially for the input "string")
wouh!! thanks
Thanks
I needed this feature before 3 year, Thanks !