bschultz Posted December 30, 2020 Share Posted December 30, 2020 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! Quote Link to comment https://forums.phpfreaks.com/topic/311950-broadcast-calendar-week-number/ Share on other sites More sharing options...
requinix Posted December 30, 2020 Share Posted December 30, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311950-broadcast-calendar-week-number/#findComment-1583541 Share on other sites More sharing options...
bschultz Posted December 30, 2020 Author Share Posted December 30, 2020 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/311950-broadcast-calendar-week-number/#findComment-1583548 Share on other sites More sharing options...
requinix Posted December 30, 2020 Share Posted December 30, 2020 What is the value of $first and $second? Quote Link to comment https://forums.phpfreaks.com/topic/311950-broadcast-calendar-week-number/#findComment-1583549 Share on other sites More sharing options...
Strider64 Posted December 31, 2020 Share Posted December 31, 2020 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/311950-broadcast-calendar-week-number/#findComment-1583550 Share on other sites More sharing options...
Solution Barand Posted December 31, 2020 Solution Share Posted December 31, 2020 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/311950-broadcast-calendar-week-number/#findComment-1583557 Share on other sites More sharing options...
bschultz Posted December 31, 2020 Author Share Posted December 31, 2020 Barand, thank you...worked great. Quote Link to comment https://forums.phpfreaks.com/topic/311950-broadcast-calendar-week-number/#findComment-1583567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.