Question

According to the ISO standar, the 52nd week of 2013, when the week starts on Saturday, starts 2013-12-28 and ends 2014-01-03 (inclusive).

Code:

echo date('Y-m-d', strtotime("2013-W52-6"));//prints 2013-12-28

Now, I am trying to figure out the inverse using PHP. If I have the date 2014-01-03, how to know the ISO weeknumber of that date if the week starts on Saturday?

Was it helpful?

Solution

Use the DateTime class:

  1. createFromFormat() to create the object based on the string you have
  2. then format('W')

OTHER TIPS

$date = "2013-12-28";
$dayofyear = date("z", $date);

$week = floor($dayofyear / 7) + 1;

// I assume week starts with Monday (I think as ISO does?)
// so only weeks that start on sunday will count as "week 0" instead of "week 1"
// I could be wildly wrong, adjust according to your needs
$first_day = date("w", date("Y", $date) . "-01-01");
if ($first_day == 6)
  --$week;

echo $week;
$date = "2013-12-28";
$date_array = explode("-", $date);
$date  = mktime(0, 0, 0, $date_array[1], $date_array[2], $date_array[0]);
$week_number  = date('W', $date);
echo $week_number;

Something like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top