I wanted to set one date to the first week of the year
that i would pass to a function as argument. I mean if my date is 03/18/2015
and i wish to set this date to the first week of the year the result should be: 12/31/2014
(this is the first week of the first date [03/18/2015] ). This is the code im trying but when change the year of the date gives me one previous or the next week of the first week of year:
$actualDate = new DateTime("03/18/2015");
$actualDate = setFirstWeekOfYear($actualDate);
function setFirstWeekOfYear( $currentDate )
{
// this variable will contain the current week number
$currentWeek = $currentDate->format("W");
// Get the current year (2015 in this moment)
$currentYear = $currentDate->format("Y");
// Rest the weeks number to the current date
$currentDate = $currentDate->modify("-{$currentWeek} week");
return $currentDate;
}
// 03/18/2017 => the output is 12/31/2016
// 03/18/2015 => the output should be 12/31/2014 but what i'm getting is 12/24/2014
Note: with the date 03/18/2017 works well but with 03/18/2015 is giving me one week previous to the first week of the year. I'm taking java Calendar.WEEK_OF_YEAR, 1
function as reference
Thanks in advance :)