martinezjc
martinezjc

Reputation: 3555

Set the first week of specific year PHP

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 :)

Upvotes: 1

Views: 1498

Answers (2)

Vince
Vince

Reputation: 1851

By doing some tests on http://writecodeonline.com/php/ ... I found that if you did:

$currentWeek = $currentDate->format("W") - 1; // subtract 1

that would produce the 31st as the date ... I think the reasoning has to do with the fact that you don't want to include the current week of the $actualDate. EG: If March 18, 2015 = the 12th week ... subtract 11 weeks before the 12th week.

Try it out, maybe that'll work for you?


UPDATE (Revised)


Look at the comments for an explanation:

$actualDate = new DateTime("03/18/2016");
$actualDate = setFirstWeekOfYear($actualDate);

function setFirstWeekOfYear($currentDate)
{
  // Grab year of current date
  $currentYear = $currentDate->format("Y");

  // Make a new DateTime variable and set the date
  $date = new DateTime();
  $date->setISODate($currentYear, 1, -1);

  // This should give you the Monday of the
  // first week that the year starts on
  return $date;
}

// 03/19/2018 = Saturday, December 30, 2017
// 03/19/2017 = Saturday, December 31, 2016
// 03/19/2016 = Saturday, January 02, 2016
// 03/19/2015 = Saturday, December 27, 2014
// 03/19/2014 = Saturday, December 28, 2013
echo $actualDate->format("l, F d, Y");

Upvotes: 0

vascowhite
vascowhite

Reputation: 18440

The DateTime class understands ISO week numbering, so you can do something like this:-

function getFirstWeekOfYear(\DateTime $date = null)
{
    if(!$date){
        $date = new \DateTime();
    }
    return (new \DateTime())->setISODate((int)$date->format('o'), 1, $date->format('w'));
}

You should note that the ISO 8601 definition for week 01 is the week with the year's first Thursday in it1.

A working example.

1: ISO week date
2: PHP date format strings

I am not a Java programmer, but a quick bit of googling suggests to me that Java does not use ISO week numbers, so may be giving you the wrong result. This question and answers may help you further.

Upvotes: 2

Related Questions