Jump to content

Broadcast Calendar Week Number


bschultz
Go to solution Solved by Barand,

Recommended Posts

The broadcast industry (radio and TV) has a special calendar.  The week starts on Monday.  Week #1 is ALWAYS the week with January 1st.

So, if January 1st is on a Sunday, week #1 will include December 26th through January 1st.

This differs from the ISO standard used by PHP.  I don't know where to start to write a function to give me the broadcast calendar week numbers for a given date.  Google didn't have much...and Stack Overflow had a couple examples...but none of them worked.  They all showed today being week #53, where today (December 30) is in week #1.

Any help would be appreciated.

Thanks...happy New Year!

Link to comment
Share on other sites

Do you only need a hint in the right direction?

You can get the first day of week #1 by asking strtotime for "January 2 last Monday". That works because
a) If January 2 is on a Monday then "last Monday" will go back a week - which is what you want because Jan 1 would be Sunday and thus part of the previous week
b) If January 2 is on Tuesday-Sunday (note that span is all within the same week) then "last Monday" will go to the previous Monday - which is going to be the same week that January 1 is because it is sometime between Monday-Saturday

Link to comment
Share on other sites

This is always echoing FIRST...even for today (12/30)...any ideas why?

 

<?php
$year = date('Y');
$yearplus = ($year + 1);

$first = date('m-d', strtotime("12-25"));
$second = date('m-d', strtotime("01-01"));



if (date('m-d') <= $first || date('m-d') >= $second)
{
$first_monday = date('Y-m-d', strtotime("January 2 $year last Monday")); echo "FIRST";
}
else
{
$first_monday = date('Y-m-d', strtotime("January 2 $yearplus last Monday"));  echo "SECOND";
}

echo $first_monday; exit;


?>

 

Link to comment
Share on other sites

I don't know if I'm on the right track and I use DateTime(), but I'm sure it can be done with date as well.

<?php
$firstMonday = new DateTime("January 1, 2021", new DateTimeZone("America/Detroit"));

if ($firstMonday->format("l") === "Sunday") {
    $firstMonday->modify("last Monday");
    echo $firstMonday->format("F j, Y -l") . "<br>";
} else {
    $firstMonday->modify("Monday");
    echo $firstMonday->format("F j, Y -l");
}

 

Link to comment
Share on other sites

  • Solution

Here's my attempt

function TVWeek($d = null)
{
    $dt = new DateTime($d);
    $y = $dt->format('Y');
    $mon1 = new DateTime("$y-01-01");

    // last week of year condition
    ++$y;
    $nextd1 = new DateTime("$y-01-01");
    if ($dt->format('W') == $nextd1->format('W')) 
        return 1;

    if ($mon1->format('w') != 1) {
        $mon1->modify('last monday');
    }
    return intdiv( $mon1->diff($dt)->days, 7 ) + 1;
}

echo '<br>' . TVWeek();                                     // 1
echo '<br>' . date('W');                                    // 53

echo '<br>' . TVWeek('2020-06-01');                         // 23
echo '<br>' . date('W', strtotime('2020-06-01'));           // 23

echo '<br>' . TVWeek('2016-06-01');                         // 23
echo '<br>' . date('W', strtotime('2016-06-01'));           // 22

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.